“Property name”必须声明主体,因为它未标记为 abstract 或 extern。 自动实现的属性必须定义 get 和 set 访问器。
除非常规属性标记为 abstract
或 extern
,或者是 partial
类型的成员,否则它必须提供主体。 自动实现的属性不提供访问器主体,但它们必须指定这两个访问器。 若要创建只读自动实现的属性,请将 set 访问器 private
设为 。
更正此错误
示例
下面的示例生成 CS0840:
// cs0840.cs
// Compile with /target:library
using System;
class Test
{
public int myProp { get; } // CS0840
// to create a read-only property
// try the following line instead
public int myProp2 { get; private set; }
}