c# - Method you can only run in a constructor -
this question has answer here:
is there way mark method in c# can run in constructor? basically, have load of read string arrays need update base columns in constructor following:
public cmsblogcontext(icmsrelationshipcontext relationshipcontext) { _relationshipcontext = relationshipcontext; columnnames.addbasecolumns(ref _blogauthorcolumns); columnnames.addbasecolumns(ref _blogcardcolumns); columnnames.addbasecolumns(ref _blogcategorycolumns); columnnames.addbasecolumns(ref _blogpagecolumns); columnnames.addbasecolumns(ref _blogpostcolumns); }
i thought tidy moving columns separate function so:
public cmsblogcontext(icmsrelationshipcontext relationshipcontext) { _relationshipcontext = relationshipcontext; addbasecolumns(); } private void addbasecolumns() { columnnames.addbasecolumns(ref _blogauthorcolumns); columnnames.addbasecolumns(ref _blogcardcolumns); columnnames.addbasecolumns(ref _blogcategorycolumns); columnnames.addbasecolumns(ref _blogpagecolumns); columnnames.addbasecolumns(ref _blogpostcolumns); }
but complained read can updated in constructor, there way make method able run in constructor (or rather, there way put read updates method not in constructor run in constructor)?
one thing use base class , put properties on that. use base class' constructor set read fields so:
public class test:testbase { public test(): base() { } public string getexample() { return example; } } public class testbase { protected readonly string example = ""; public testbase() { example ="hi"; } }
i can use , works:
var test = new test(); console.writeline(test.getexample());
that way hide away fields enforce constructor accessiblity of read fields.
Comments
Post a Comment