我有一些代码需要在调试和发布模式下略有不同。它有一个常量,在某些模式和其他模式PrettyPrint
中设置为,我有时会更改它们。true
false
#if DEBUG
public const bool PrettyPrint = true;
#else
public const bool PrettyPrint = false;
#endif
// ...snip...
string start, end, comma, innerIndentation;
if (Printer.PrettyPrint) {
innerIndentation = indentation + " ";
start = "[\n";
end = indentation + "]";
comma = ",\n" + innerIndentation;
} else {
innerIndentation = "";
start = "[";
end = "]";
comma = ",";
}
// Then do some prints using the initialized strings as constants
这很好用,编译器足够聪明,可以优化if
离开。但是,我收到一个烦人的警告:
warning CS0162: Unreachable code detected
有没有办法在不执行以下任何操作的情况下避免此警告:
- 直接在代码中使用
#if
- 因为它使那部分代码非常难看,我想尽可能避免#if
s 。 - 在其他情况下抑制 CS0162 - 因为我发现该警告对于查找损坏的代码非常宝贵。
如何#if DEBUG
在 IDE 不相信后面的所有代码都无法访问的情况下使用语句?