c# - Simple Syntax for Declaring properties with a starting value -


well far, shortest code i've seen declare property can set inside class i've seen is:

public t property {get; private set;}  

but if want declare starting value (which not default value type), how it??

actually i'm doing this:

public t property {get; private set;} private void initialize() {property = value; } 

another option is:

private t _property = value; public property {get {return _property;}} 

but i'm wondering if can write 1 code line only, because i'll writing many of properties, , don't want have duplicate line each.

nope. auto-properties always default default value.

your best bet set them in constructor, or else not use auto-property.

public t property {get; private set;} public myclass() {    property = value;  } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -