C# 修饰符readonly对于get only属性无效

C# 修饰符readonly对于get only属性无效,c#,C#,我尝试实现singleton public static readonly MyClass Instance { get; } = new MyClass(); 然而VisualStudio告诉我,我不能对此项使用readonly。这是为什么?readonly是字段的有效修饰符 对于属性,您可以使用get和set控制是否以及谁可以写入/读取readonly对属性没有意义。只读属性仅与属性一起使用,您只能在构造函数内的运行时设置值。

我尝试实现singleton

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

然而VisualStudio告诉我,我不能对此项使用readonly。这是为什么?

readonly
是字段的有效修饰符


对于属性,您可以使用
get
set
控制是否以及谁可以写入/读取
readonly
对属性没有意义。

只读属性仅与属性一起使用,您只能在构造函数内的运行时设置值。