C# getter-only 属性的默认值

2024年9月5日 | 1 分钟阅读

此功能允许我们为 getters 设置默认值。getter-only 属性是只读属性。我们无法为此属性设置新值。

如果我们在编译时显式赋值,编译器将报告错误:无法赋值

C# getter-only 属性的默认值 示例 1

输出

Rahul kumar
[email protected]

让我们看一个例子,如果我们显式赋值会发生什么。

C# getter-only 属性的默认值 示例 2

输出

error CS0200: Property or indexer 'Student.Name' cannot be assigned to -- it is read only