While making use of the new automatic property feature of C# 3.0, the compiler forces you to declare both the getter and the setter. While this is fine for mutable properties, obviously you can't create a read-only property by omitting the setter like you would before. You might opt for the
readonly modifier but it does not apply to properties. Fortunately, since C# 2.0 you can provide a specific access modifier for each getter and setter. This way you can make the property read only to the world while you can set its value from within your class without being forced to use a property backer:
public class MyClass
{
public void MyReadonlyProperty
{
get;
private set;
}
public MyClass()
{
MyReadonlyProperty = 123;
}
}
0 comments:
Post a Comment