简体   繁体   English

快速简单的二传手和吸气剂?

[英]quick and easy setters and getters?

It's allowed to do: 允许这样做:

public int Age
{
get;
set;
}

but does the application create/allocate the space for the variable? 但应用程序是否为变量创建/分配空间? I usually do 我经常这样做

private int age = 0;
public int Age
{
get { return this.age; }
set { this.age = value; }
}

Yes it does. 是的,它确实。 If you looked at the IL you'll see that it creates a backing variable for the property. 如果您查看IL,您将看到它为该属性创建了一个支持变量。

The compiler will automatically generate the backing field at compile time if it finds empty get or set blocks saving you the work. 如果编译器找到空的get或set块,则会在编译时自动生成支持字段,从而为您节省工作。 You can still add get and set blocks that have additional filtering logic in them as well although you'll have to type all of that yourself of course. 您仍然可以在其中添加get和set块以及其他过滤逻辑,尽管您当然必须自己键入所有这些。

See here for more details about Auto Properties . 有关自动属性的详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM