3

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; }
}
CharithJ
  • 46,289
  • 20
  • 116
  • 131
Jason94
  • 13,320
  • 37
  • 106
  • 184

2 Answers2

6

Yes it does. If you looked at the IL you'll see that it creates a backing variable for the property.

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
  • 1
    @Jason94 - Backing up Tomas's answer, Bart De Smet has [a very thorough blog post](http://community.bartdesmet.net/blogs/bart/archive/2007/03/03/c-3-0-automatic-properties-explained.aspx) with all the detailed info you could want. – Rick Liddle Aug 08 '11 at 12:07
2

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.

CharithJ
  • 46,289
  • 20
  • 116
  • 131