使用const布尔值时的编译器警告(Compiler warning when using const boolean)

编程入门 行业动态 更新时间:2024-10-26 03:29:45
使用const布尔值时的编译器警告(Compiler warning when using const boolean)

我偶然发现了一个令人惊讶的编译器警告,使用下面的代码:

private const bool DEFAULT_SETTING = false; //... string aString = (DEFAULT_SETTING) ? "1" : "0";

这会生成警告warning CS0429: Unreachable expression code detected 。

在最初感到困惑之后,我意识到编译器没有错,因为DEFAULT_SETTING的值是不变的,不能改变。 所以? "1" 永远不可能到达三元运算符的? "1"部分。 但编译器也没有完全正确,因为我或未来的开发人员可能需要(或需要)在不破坏代码的情况下更改默认值。

有没有更好的方法来使用类似于上述不生成警告的上下文的默认设置?

注意:您可能想知道为什么我想将false转换为"0" ...它将保存到设置XML文件。 当文件丢失时,软件会自动生成一个闪亮的新XML文件,其中包含默认设置。

I stumbled on a surprising compiler warning using the following code:

private const bool DEFAULT_SETTING = false; //... string aString = (DEFAULT_SETTING) ? "1" : "0";

which generates the warning warning CS0429: Unreachable expression code detected.

After initially being puzzled, I realised the compiler is not wrong because the value of DEFAULT_SETTING is constant and cannot change. Therefore the ? "1" part of the ternary operator can never be reached. But neither is the compiler completely right because myself or a future developer may want (or need) to change the default value without breaking the code.

Is there better way to use a default setting similar to the above context which does not generate the warning?

Note: You may want to know why I would want to convert false into "0"...it is to save to a settings XML file. When the file is missing, the software automatically generates a shiny new XML file with default settings.

最满意答案

有没有更好的方法来使用类似于上述不生成警告的上下文的默认设置?

是。

但首先:现在停止使用SHOUTY_SNAKE_CASING 。 它使它看起来像你是一个C程序员。 C#使用camelCasing和PascalCasing。

现在你的问题。 如果一个命名值可以通过定义改变,那么它不是一个常量 。 只使用常量来保证永不改变的事物 。 Pi是一个常数。 黄金原子中的质子数量是一个常数。 程序的名称不是常量,不管这是调试还是零售版本不是常量,等等。 那些事情变了。

编译器有权向您发出有关您的代码的警告,因为它似乎是基于永不改变的东西做出选择,因此可能是一个错误。

这不仅仅是一个公约; C#语言的语义假设一个常量不会改变。 例如,如果在Foo.DLL中有一个公用的常量Foo ,并且它被Bar.EXE中的代码占用,则为Foo重新编译具有不同值的Foo.DLL并不会自动在Bar.EXE中更新Foo的值。 C#编译器将假定Foo 永远不会更改并在Bar.EXE中复制其值。

为了表示“这个东西在我的程序中没有改变,但它可以在我的程序的不同实例中改变”的概念,请使用readonly :

private static readonly bool DefaultSetting = false; ... string aString = DefaultSetting ? "1" : "0";

Is there better way to use a default setting similar to the above context which does not generate the warning?

Yes.

But first: stop using SHOUTY_SNAKE_CASING right now. It makes it look like you're a C programmer. C# uses camelCasing and PascalCasing.

Now to your question. If a named value can change then by definition it is not a constant. Use constants only for things which never change. Pi is a constant. The number of protons in an atom of gold is a constant. The name of your program is not a constant, whether this is the debug or retail build is not a constant, and so on. Those things change.

The compiler is within its rights to warn you about your code because it appears to be making a choice based on something which never changes, and therefore is likely a bug.

This isn't just a convention; the semantics of the C# language assume that a constant will not change ever. For example, if you have a public constant Foo in Foo.DLL, and it is consumed by code in Bar.EXE, recompiling Foo.DLL with a different value for Foo does not update the value of Foo automatically in Bar.EXE. The C# compiler will assume that Foo will never change and makes a copy of its value in Bar.EXE.

To represent the concept "this thing doesn't change in my program, but it could change in a different instance of my program" use readonly:

private static readonly bool DefaultSetting = false; ... string aString = DefaultSetting ? "1" : "0";

更多推荐

本文发布于:2023-07-20 12:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编译器   布尔值   const   boolean   warning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!