加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 设置自动实现属性的默认值

发布时间:2020-12-15 06:48:29 所属栏目:百科 来源:网络整理
导读:参见英文答案 Initializing C# auto-properties 4个 假设我有一个自动实现的属性 public int SheetNum { get; set; } 无论如何将SheetNum的默认值设置为1,所以它会是这样的 private int sheetNum = 1;public int SheetNum{ set { this.sheetNum = value; } g
参见英文答案 > Initializing C# auto-properties 4个
假设我有一个自动实现的属性
public int SheetNum { get; set; }

无论如何将SheetNum的默认值设置为1,所以它会是这样的

private int sheetNum = 1;

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

解决方法

你快到了;你只需要在构造函数中初始化值:
public class MyClass
{
    public MyClass()
    {
        Foo = 1;
    }

    public int Foo { get; set; }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读