In C# 1.0 a property's get and set accessors always shared the same access level. If you wanted a publicly readable but privately writable property you had to fall back to a read-only property with a separate private method or field to set the value.
C# 2.0 allows one of the accessors to have a more restrictive access modifier than the property itself.
Code
C#
class Account
{
public string Name { get; private set; }
public Account(string name)
{
Name = name;
}
}C#
class Account
{
private string name;
public string Name
{
get { return name; }
}
public Account(string name)
{
this.name = name;
}
}Notes
- Only one accessor can have a different access level, and it must be more restrictive than the property itself
- Common combinations are
public get/private setandpublic get/protected set - This feature is used extensively with Auto-implemented properties C# 3.0