简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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