-1

I try to implement singleton

public static readonly MyClass Instance { get; } = new MyClass();

However Visual Studio tells me, I cannot use readonly for this item. Why is that?

Cencek
  • 33
  • 5
  • Read only property is used with properties only, in which you can only set value on run time inside the constructor. – farrukh aziz Aug 16 '18 at 04:14

1 Answers1

4

readonly is a valid modifier for fields.

For properties you can control if and who can write/read with get and set. readonly doesn't make sense on a property.

bolov
  • 72,283
  • 15
  • 145
  • 224