Properties in C# were unnecessarily verbose when exposing an internal field in a future-proof way1.
C# 3.0 provides a more concise syntax to achieve the common use cases where a property simply maps to an private field.
Code
C#
class Contact
{
public Guid Id { get; private set; }
public string Name { get; set; }
}
C#
class Contact
{
private Guid id;
private string name;
public Guid Id
{
get { return id; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
Notes
- Auto-implemented properties don't allow for change-notification hooks like
INotifyPropertyChange
- Read-only auto-implemented properties can't have defaults until Auto property initializers C# 6.0
1 Exposing a field in a public type and changing it to a property later is a binary breaking change.