Simple syntax for declaring properties with a start value

So far, the shortest code I've seen to declare a property that can only be set inside the class I saw:

public T Property {get; private set;} 

But what if I want to declare it already with a start value (which is not the default value for this type), how can I do this?

I actually do this:

 public T Property {get; private set;} private void Initialize() {Property = Value; } 

Another variant:

 private T _Property = Value; public property {get {return _Property;}} 

But I am wondering if I can only write this with ONE code line, because I will write many of these properties, and I do not want to have a duplicate line for each.

+4
source share
1 answer

Nope. Auto properties are always the default.

It is best to set them in the constructor or just not use the auto-property.

 public T Property {get; private set;} public MyClass() { Property = Value; } 
+4
source

Source: https://habr.com/ru/post/1479794/


All Articles