quick and easy setters and getters?
It's allowed to do:
public int Age
{
get;
set;
}
but does the application create/allocate the spa开发者_如何学Pythonce 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.
The compiler will automatically generate the backing field at compile time if it finds empty get or set blocks saving you the work. 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.
See here for more details about Auto Properties.
精彩评论