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
  • 206,299
  • 34
  • 391
  • 426
Wusiji
  • 599
  • 1
  • 6
  • 23

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,003
  • 7
  • 56
  • 67