c# - 条件 ("Debug") + #if DEBUG

标签 c# .net vb.net

我在 Debugging .net 2.0 Applications 中看到了以下代码

[Conditional("DEBUG")]
void AssertTableExists() {
    #if DEBUG
        ...
    #endif
}

是否有理由使用#if 指令?我的意思是,据我所知,只有在定义了 DEBUG 时才会调用该方法,所以我看不出在方法主体中包含 #if 有什么意义。

最佳答案

巧合的是,我上周刚好在我的博客上回答了你的问题。

http://ericlippert.com/2009/09/10/whats-the-difference-between-conditional-compilation-and-the-conditional-attribute/

如果方法的主体引用了自己在#if DEBUG 指令下声明的实体,则#if 指令是必需的。例如

#if DEBUG
static private int testCounter = 1;
#endif

[Conditional("DEBUG")] void CheckConsistency()
{
#if DEBUG
  testCounter++;
#endif
...

如果您在方法主体中省略了 #if DEBUG,则不会在发布版本中编译。

关于c# - 条件 ("Debug") + #if DEBUG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452335/

相关文章:

c# - 从其orderby子句中清除IQueryable

c# - SQL Server 2008 日期时间字段溢出

c# - WCF 中的事件委托(delegate)类比

.net - 如何使用 DataReceived 事件在 .Net 中使用 SerialPort?

.net - 如何将字符串连接到 MVC4 中模型返回的数据

html - 将 html 表解析为数据集时出错

c# - WPF 数据网格 : How do I access a ComboBox in a specific row of a DataGridTemplateColumn?

c# - 如何通过c#mvc函数传递两个参数来调用存储过程

c# - 多目标 .NET Framework 4 和 Visual Studio 2012

asp.net - 将 "camel case"类型的文本转换为中间有空格的文本的函数?即 : HelloWorld --> Hello World