class myClass{ int age; public int Age{ get{return age;} set{age = value;}}Versusclass myClass{ public int Age{get; set;}}What's the difference between these two? Are they both the same?
What's the difference between these property definitions in C#
c#
They both compile to the same IL.An automatic property is simply shorthand for the same thing with a private backing field (your first example).From Auto-Implemented Properties on MSDN:When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.As @svick commented, the obvious difference is that you can access the backing field directly in your first example, whereas you can't in the second (as it is a generated, anonymous field).