且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# 中的自动属性是什么?它们的用途是什么?

更新时间:2021-11-29 03:54:30

当属性访问器中不需要额外的逻辑时,使用自动属性.
声明看起来像这样:

Automatic Properties are used when no additional logic is required in the property accessors.
The declaration would look something like this:

public int SomeProperty { get; set; }

它们只是语法糖,因此您无需编写以下更冗长的代码:

They are just syntactic sugar so you won't need to write the following more lengthy code:

 private int _someField;
 public int SomeProperty 
 {
    get { return _someField;}
    set { _someField = value;}
 }