user496949
user496949

Reputation: 86145

readonly modifier

can we use readonly to modify property? If not, why?

Upvotes: 1

Views: 602

Answers (3)

Dean Chalk
Dean Chalk

Reputation: 20471

Is your question 'can you modify a readonly field', then your answer is YES.

However, like previous replies have stated Properties are equivalent to methods, so readonly doesnt apply.

Upvotes: 0

jdehaan
jdehaan

Reputation: 19938

To create a read only property just make the setter private. If you want the property also to be read only from within the class, then remove the setter and return a read-only member.

The readonly keyword can only be applied to fields. That is the only usage foreseen by the C# specification at the moment.

Upvotes: 4

James Kovacs
James Kovacs

Reputation: 11661

You don't need readonly for properties. If they're readonly, don't declare a setter. If you're using auto-properties, declare the setter as private.

Upvotes: 16

Related Questions