【问题标题】:What is the difference between these properties declarations? [duplicate]这些属性声明之间有什么区别? [复制]
【发布时间】:2015-12-14 08:09:09
【问题描述】:

我看到我可以通过两种方式声明公共属性。两者都有get/set访问器,但是它们有什么区别呢?

class Job
{
    public int Interval { get; set; }
    public string Key { get; set; }
}

class Job1
{
    public int Interval = 0;
    public string Key = string.Empty;
}

【问题讨论】:

  • 我可以通过两种方式声明公共属性——这是完全错误的,除非你的意思是成员。跨度>
  • 你在破坏我的积分!开个玩笑:)我现在明白了

标签: c# c#-4.0


【解决方案1】:

第一个示例是 property - 它声明了 gettersetter 方法。

第二个例子是公开的field,而不是property。公共字段是不好的编码习惯。

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 2016-05-08
    • 2021-04-19
    • 1970-01-01
    • 2020-07-22
    相关资源
    最近更新 更多