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

时间:2011-08-08 11:54:43

标签: c# .net automatic-properties

允许这样做:

public int Age
{
get;
set;
}

但应用程序是否为变量创建/分配空间?我通常做

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

2 个答案:

答案 0 :(得分:6)

是的。如果您查看IL,您将看到它为该属性创建了一个支持变量。

答案 1 :(得分:2)

如果找到空的get或set块,编译器将在编译时自动生成支持字段,从而为您节省工作。您仍然可以在其中添加get和set块以及其他过滤逻辑,尽管您当然必须自己键入所有这些。

有关Auto Properties的详细信息,请参阅此处。