简体   繁体   English

为什么此C#代码具有未声明的get主体构建? 以及如何使编译器对此产生错误?

[英]Why does this C# code with an an undeclared get body build? And how can I make the compiler throw an error on this?

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. 我不希望它会编译,因为MyChild类没有为重写的MyBool getter声明主体。 I tried marking the MyChild class sealed , but it still compiled. 我尝试将MyChild类标记为sealed ,但仍可以编译。 My first question is simply, why is the MyChild class not required to provide a body for the MyBool getter? 我的第一个问题很简单,为什么不要求MyChild类为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. “ MyChild.MyBool.get”必须声明一个主体,因为它没有被标记为抽象或外部。 Automatically implemented properties must define both get and set accessors. 自动实现的属性必须定义get和set访问器。

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? 我的第二个问题是:当我尝试编译此代码时,可以检查哪些设置以确保编译器向我发出此警告?

I wouldn't expect this to compile because the MyChild class doesn't declare a body for the overridden MyBool getter. 我不希望它会编译,因为MyChild类没有为重写的MyBool getter声明主体。

That is because it is perfectly fine. 那是因为它很好。 The MyBool property has an (implicit) setter, but that is only available to the constructor. MyBool属性具有一个(隐式)设置器,但仅对构造方法可用。 This is a new feature to the C# 6 compiler, so your build server needs the same version of the compiler to make this work. 这是C#6编译器的新功能,因此您的构建服务器需要相同版本的编译器才能完成此工作。

There is no need to throw an exception, or anything like that. 不需要抛出异常或类似的东西。 If you want a set to be declared, just make sure to add it to the property definition in the base class. 如果要声明一个set ,只需确保将其添加到基类的属性定义中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使C#抛出解码异常? - How can I make C# throw decoding exceptions? 为什么C#编译器会在此代码上崩溃? - Why does the C# compiler crash on this code? 为什么C#编译器在catch中授权“throw ex”,是否存在“throw ex”有用的情况? - Why does the C# compiler authorize “throw ex” in catch, and is there a case where “throw ex” is useful? 我在 C# 上遇到编译器错误你能帮帮我吗? - I get Compiler Error on C# Can you help me? 为什么javascript indexOf()方法在从C#代码隐藏调用时抛出错误? - Why does javascript indexOf() method throw error when called from C# code-behind? 为什么此 C# 代码会引发错误:使用未分配的局部变量 'n' - Why does this C# code throw an error: Use of unassigned local variable 'n' 为什么我的代码会引发无效的强制转换异常? (C#)? - Why does my code throw an Invalid Cast Exception? (C#)? C#switch variable initialization:为什么这段代码不会导致编译错误或运行时错误? - C# switch variable initialization: Why does this code NOT cause a compiler error or a runtime error? 为什么C#中的这段代码不会引发错误 - Why doesn't this code in C# throw an error 为什么C#编译器从此代码创建PrivateImplementationDetails? - Why does the c# compiler create a PrivateImplementationDetails from this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM