c# – Defines.Debug vs #if DEBUG
发布时间:2020-12-15 06:44:05 所属栏目:百科 来源:网络整理
导读:我已经开始使用像这样的定义类: 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 } 我看到的优点是: >确保我不会打破#if ..#else ..#endif不会被编译器检查的东西. 可能的劣势我看到: 如果“定义”类在另一个程序集中,编译器将无法优化未使用的代码.这是为什么我做了内部的. 我没有任何其他的缺点吗? [编辑]典型用法示例: private readonly Permissions _permissions = Defines.Debug ? Permissions.NewAllTrue() : Permissions.NewAllFalse(); 要么: var str = string.Format(Defines.Debug ? "{0} {1} ({2})" : "{0} {1}",actual,text,advance); 解决方法
我看到至少有一个很大的缺点:如果Debug是false,这段代码会引发一个警告:
if (Debug) Console.WriteLine("Debug"); 因为编译器会检测到该条件是永远不会满足的,所以Console.WriteLine调用是不可访问的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |