Differences:
- The second form will only be compiled using the C # 3 compiler or later
- The second form does not allow any code (even in the same class) to directly access the field, since the real field has an "inexpressible name"
The second version is what is known as an automatically implemented property (or “automatic property” for short). They were introduced in C # 3. If you are only writing code that looks like the first version, that is, there is no logic, then the automatic properties are great. You can always add logic later by translating it into the first form. All of your code will be compatible with this change in both the source and binary compatibility conditions.
Remember that automatic properties do not allow you to specify default values, and there is no such thing as a truly automatic readonly property (i.e. one without a getter). The closest you can become a public recipient with a private setter, for example.
public string Name { get; private set; }
This is not quite the same, but it is close enough in many situations.
Jon Skeet Feb 07 '11 at 16:23 2011-02-07 16:23
source share