public string MyProp { get; }- This is introduced in C # 6.0. . Such properties are called read-only automatic properties. Assignments to such members can only be performed as part of a declaration or in a constructor of the same class. You can read a detailed explanation about this in this MSDN article or in the Jon Skeet blog. As explained in this article, this property automatically solves four problems:
public string MyProp { get; private set; } - , , .
, auto-properties , auto-initialize, # 6.0:
public string MyProp { get; } = "You can not change me";
#:
private readonly string myProp = "You can not change me"
public string MyProp { get { return myProp ; } }
, # 6.0:
public string MyProp { get; }
protected MyClass(string myProp, ...)
{
this.MyProp = myProp;
...
}
:
private readonly string myProp;
public string MyProp { get { return myProp; } }
protected MyClass(string myProp, ...)
{
this.myProp = myProp;
...
}