开发者

quick and easy setters and getters?

开发者 https://www.devze.com 2023-03-26 10:01 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号