我来试试看。
看起来这可能只是Intellisense的一个bug,它无法找到auto属性的基本实现。代码是有效的,而且很有意义——下面是另一种表达示例的方法。
Child child = new Child();
child.SetProperty(true);
class Parent
{
private bool _property;
public virtual bool GetProperty() => _property;
public virtual void SetProperty(bool value) => _property = value;
}
class Child : Parent
{
public override bool GetProperty() => base.GetProperty();
}
有了这个表示,现在很明显为什么重写GetProperty是好的。以下是代码的相关IL:
Main:
IL_0000: newobj Child..ctor
IL_0005: ldc.i4.1
IL_0006: callvirt Parent.set_Property
IL_000B: ret
Parent.get_Property:
IL_0000: ldarg.0
IL_0001: ldfld Parent.<Property>k__BackingField
IL_0006: ret
Parent.set_Property:
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld Parent.<Property>k__BackingField
IL_0007: ret
Parent..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: ret
Child.get_Property:
IL_0000: ldarg.0
IL_0001: call Parent.get_Property
IL_0006: ret
Child..ctor:
IL_0000: ldarg.0
IL_0001: call Parent..ctor
IL_0006: ret
以下是我的版本:
Main:
IL_0000: newobj Child..ctor
IL_0005: ldc.i4.1
IL_0006: callvirt Parent.SetProperty
IL_000B: ret
Parent.GetProperty:
IL_0000: ldarg.0
IL_0001: ldfld Parent._property
IL_0006: ret
Parent.SetProperty:
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld Parent._property
IL_0007: ret
Parent..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: ret
Child.GetProperty:
IL_0000: ldarg.0
IL_0001: call Parent.GetProperty
IL_0006: ret
Child..ctor:
IL_0000: ldarg.0
IL_0001: call Parent..ctor
IL_0006: ret
请注意,这与
public override bool Property { get; }
,是指示编译器为同名的backing属性生成单个getter重写的简写,没有提及先前存在的setter。然而,有实际规范经验的人肯定能够提供更多关于这方面的信息。