Set default value for automatically implemented property

Say I have a property with auto-implementation

public int SheetNum { get; set; } 

In any case, to set the default SheetNum to 1, it will look like

 private int sheetNum = 1; public int SheetNum { set { this.sheetNum = value; } get { return this.sheetNum; } } 
+6
source share
1 answer

You're almost there; you just need to initialize the value in the constructor:

 public class MyClass { public MyClass() { Foo = 1; } public int Foo { get; set; } } 
+12
source

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


All Articles