3

This question already has an answer here:

Why properties in C# cannot be readonly ?

When I try to have a property readonly it states that:

a modifier 'readonly' is not valid for this item

Simmilar question was asked here: Why can't properties be readonly? But the question was asked 5 years ago, and the answer provided then was: Because they didn't think it thru. Is this still the case after 5 years?

edit: Code example:

public class GreetingClass
{
    public readonly string HelloText { get; set; }
}


  • could you please post your code - sujith karivelil
  • Well, technically nothing changed after that 5 years, and all answers to question you've mentioned are still up to date. - Andy Korneyev
  • Well, property is just a convenient name for get/set accessors which a methods. - Dmitry Bychenko
  • See my answer. It compares C# 6.0 and prior versions. Things have changed a bit in C# 6.0. - Nikhil Vartak

5 답변


14

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; }}


3

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

public MyProperty { get; }


  • does this work for all C# versions or only for C#6+ ? - Stralos
  • it will work for all later than 2.0 - Ovais Khatri

2

A property with out set is consider as a readonly property in C# you need not be specify it with a readonly key word.

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

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


  • Can return value through private variable; see updates in the anser - sujith karivelil

1

If you had googled readonly properties in C#, you would have got 100*no_of_answers_here correct results.

https://msdn.microsoft.com/en-us/library/w86s7x04.aspx clearly states, a property without set accessor is readonly. So it's logical to think that applying readonly keyword in addition would cause redundancy, nothing else.

Additionally until C# 6.0 you could not set the readonly property even withing the containing type (say, a class). In C# 6.0, you can at least do so as a part of initialization. Meaning this is possible in C# 6.0, public int Prop { get; } = 10; When you do so, a readonly backing field is automatically generated and set to the value you chose for property initialization.


0

More or less, from C#6 it s possible using something like this:

class MyClass {
    string MyProp { get; }
    MyClass { MyProp = "Hallo Worls"; }
}

Before C#6 you can use a read-only backing-field instead:

class MyClass {
    private readonly string _myProp;
    string MyProp { get { return this._myProp; } }
    MyClass { this._myProp = "Hallo Worls"; }
}

Linked


Related

Latest