Hiding automatic property getter/setter in C# 3.0 (readonly property)

Just found out how to hide the setter of an automatic property, creating a read-only version of an automatic property (quoting from the documentation):

public class ReadOnlyPoint {
   public int X { get; private set; }
   public int Y { get; private set; }
   public ReadOnlyPoint(int x, int y) { X = x; Y = y; }
}

In the same manner a writeonly property can also be created by modifying the getter, but you’re only allowed 1 access modifier on each automatic property. As well as “private”, it is also possible to mark the getter or setter as “internal”.

Advertisements
Posted in C#