省略代码:条件属性和预处理指令之间有什么区别? [英] Omitting code: Any difference between Conditional Attribute and pre-processing directive?

查看:173
本文介绍了省略代码:条件属性和预处理指令之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有什么区别在

#define MYSYMBOL

#if MYSYMBOL
public void foo () {

    // ...
}
#endif

#define MYSYMBOL

[Conditional("MYSYMBOL")]
public void foo () {

    // ...
}

也许很明显,但如果有人可以给我一个正确方向的提示,我会感激:)

Maybe it's obvious but if someone could give me a hint in the right direction I would be thankful :)

推荐答案

它们不同

使用 #if 完全删除封闭的代码,所以调用该方法的任何代码都不会编译,因为该方法已经消失。您也可以以这种方式包装任何数量的代码,而不仅仅是一个整体的方法。

Using #if removes the enclosed code altogether, so any code that calls the method won't compile because the method has disappeared. You can also wrap any amount of code this way, not just a single whole method.

使用 [条件] 意味着该方法不会在运行时被调用,但是对它的调用仍然会被编译(但是调用不会在IL代码中发出)。此外,这种方式,该方法必须返回 void ,并且没有任何 out ref 参数。

Using [Conditional] means the method won't be called at runtime, but calls to it will still compile ( but the calls will not be emitted in the IL code ). Also, this way, the method must return void and not have any out or ref parameters.

这篇关于省略代码:条件属性和预处理指令之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆