To my surprise, the following code builds and runs fine:
private abstract class MyParent
{
public abstract bool MyBool { get; }
}
private class MyChild : MyParent
{
public override bool MyBool { get; } //no body declared
}
[TestMethod]
public void MyTestMethod()
{
var mc = new MyChild();
Assert.IsFalse(mc.MyBool);
}
I don't get any warnings either. The test even passes. I wouldn't expect this to compile because the MyChild
class doesn't declare a body for the overridden MyBool
getter. I tried marking the MyChild
class sealed
, but it still compiled. My first question is simply, why is the MyChild
class not required to provide a body for the MyBool
getter?
I found that I had a problem like this in my code because my build server reported a compiler error that said:
'MyChild.MyBool.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.
The build server must be using some settings that prevented this same code from compiling. My second question is: what settings can I check to ensure that the compiler gives me this warning when I try to compile this code?