7

Say I have an auto-implemented property

public int SheetNum { get; set; }

Is there anyway to set the default value of SheetNum to 1, so it would be like

private int sheetNum = 1;

public int SheetNum
{
    set { this.sheetNum = value; }
    get { return this.sheetNum; }
}
dtb
  • 213,145
  • 36
  • 401
  • 431
Wusiji
  • 599
  • 1
  • 7
  • 24

1 Answers1

12

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

public class MyClass
{
    public MyClass()
    {
        Foo = 1;
    }

    public int Foo { get; set; }
}
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69