Why can't readonly be used with properties

Properties can be readonly in C#, the implementation is just not using the readonly keyword:

If you use C#6 (VS 2015) you can use the following line, which allows assigning the property in either the constructor or in the member definition.

public int Property { get; }

If you use an older C# / Visual Studio Version you can write something like this, and assign the field in the constructor or the field definition:

private readonly int property;
public int Property { get { return this.property; }}

If you want to keep properties read only, you may just define their getter like this:

public MyProperty { get; }

A property without set considered as a read-only property in C#, you need not specify them with a Readonly keyword.

public class GreetingClass
{
    private string _HelloText = "some text"; 
    public string HelloText => _HelloText; 
}

Whereas in VB you have to specify: Public ReadOnly Property HelloText() As String

Tags:

C#