Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 定义.Debug与#if Debug_C# - Fatal编程技术网

C# 定义.Debug与#if Debug

C# 定义.Debug与#if Debug,c#,C#,我开始使用这样的定义类: internal sealed class Defines { /// <summary> /// This constant is set to true iff the define DEBUG is set. /// </summary> public const bool Debug = #if DEBUG true; #else false; #endif } 或: 我至少看到了

我开始使用这样的定义类:

internal sealed class Defines
{
    /// <summary>
    /// This constant is set to true iff the define DEBUG is set.
    /// </summary>
    public const bool Debug =
  #if DEBUG
   true;
  #else
   false;
  #endif
}
或:


我至少看到了一个很大的缺点:如果
Debug
为false,则此代码将导致警告:

if (Debug)
    Console.WriteLine("Debug");

因为编译器将检测到该条件从未满足,因此无法访问
控制台.WriteLine
调用。

您是否查看了所有?Oded谢谢,我添加了典型示例,只是单行中的小修改,而不是ConditionalAttribute适合的整个方法或类,或者我不应该这么懒散,为这些行定义两个方法?如果包含它的程序集是内置的
Debug
,而引用它的程序集是内置的
Release
,会发生什么情况?这与我使用的#if Debug>的情况相同。您是否建议ConditionalAttribute解决这个问题,并且所有程序集都使用exe程序集的条件运行,不管它们是如何生成的?不,一点也不。但我只想强调一下任何这种构造的问题。哦,是的,这很烦人。我想我会研究一下@Oded的建议。
var str = string.Format(Defines.Debug ? "{0} {1} ({2})" : "{0} {1}", actual, text, advance);
if (Debug)
    Console.WriteLine("Debug");