7

假设我有一个自动实现的属性

public int SheetNum { get; set; }

无论如何将默认值设置SheetNum为1,所以它就像

private int sheetNum = 1;

public int SheetNum
{
    set { this.sheetNum = value; }
    get { return this.sheetNum; }
}
4

1 回答 1

12

您快到了; 你只需要在构造函数中初始化值:

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

    public int Foo { get; set; }
}
于 2013-05-27T22:08:16.527 回答