Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default value for auto-implemented property [duplicate]

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; }
}
like image 609
Wusiji Avatar asked May 27 '13 22:05

Wusiji


1 Answers

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; }
}
like image 176
oɔɯǝɹ Avatar answered Oct 04 '22 09:10

oɔɯǝɹ