C#:getter/setter
我在某处看到类似以下内容,并想知道这是什么意思。我知道它们是 getter 和 setter,但想知道为什么字符串 Type 是这样定义的。谢谢你帮助我。
public string Type { get; set; }
I saw something like the following somewhere, and was wondering what it meant. I know they are getters and setters, but want to know why the string Type is defined like this. Thanks for helping me.
public string Type { get; set; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这些是自动实现的属性(简称“自动属性”)。
编译器将自动生成以下简单实现的等效项:
Those are Auto-Implemented Properties (Auto Properties for short).
The compiler will auto-generate the equivalent of the following simple implementation:
这是一个自动属性,它是其简写符号:
That is an auto-property and it is the shorthand notation for this:
在 C# 6 中:
现在可以将自动属性声明为字段:
只读自动属性
In C# 6:
It is now possible to declare the auto-properties just as a field:
Read-Only Auto-Properties
与做没有什么不同
is no different than doing
这意味着编译器在运行时定义了一个支持字段。这是自动实现属性的语法。
详细信息: 自动-实现的属性
This means that the compiler defines a backing field at runtime. This is the syntax for auto-implemented properties.
More Information: Auto-Implemented Properties
您还可以使用 lambda 表达式
You can also use a lambda expression
它是一个自动支持的属性,基本上相当于:
It's an automatically backed property, basically equivalent to:
这些称为自动属性。
http://msdn.microsoft.com/en-us/library/bb384054.aspx
在功能上(以及就已编译的 IL 而言),它们与具有支持字段的属性相同。
These are called auto properties.
http://msdn.microsoft.com/en-us/library/bb384054.aspx
Functionally (and in terms of the compiled IL), they are the same as properties with backing fields.
随着 C# 6 的发布,您现在可以对私有属性执行类似的操作。
With the release of C# 6, you can now do something like this for private properties.