I came across an unexpected compiler warning using the following code:
private const bool DEFAULT_SETTING = false;
string aString = (DEFAULT_SETTING) ? "1" : "0";
which generates a warning warning CS0429: Unreachable expression code detected.
After I was puzzled, I realized that the compiler is not mistaken, because the value DEFAULT_SETTINGis constant and cannot change. Therefore, part of the ? "1"triple operator cannot be reached . But none of them is completely correct, because I or the future developer may (or need) to change the default value without breaking the code.
Can I use a default setting similar to the above context that does not generate a warning?
Note. You might want to know why I would like to convert falseto "0"... is to save the settings file. When a file is missing, the software automatically generates a new shiny XML file with default settings.
source
share