[英]Is it valid to use the #define preprocessor directive inside an #if in C#
Could I use #define
preprocessor directive inside #if
and #endif
, in C# ? 我可以在C#的#if
和#endif
内部使用#define
预处理程序指令吗?
eg 例如
#if !SILVERLIGHT && !__ANDROID__ && !__IOS__
#define SupportsMutex
#endif
It looks like it works, but I need to be sure. 看起来可行,但我需要确定。 There is a lot written about this, but most of the time in the context of C and not C# - the preprocessor directives a in C# far more limited. 关于此的文章很多,但是大多数时候是在C而不是C#的上下文中-C#中的预处理器指令a受到更多限制。
Visual Studio's highlighting seems to support it, but it this really valid according to the language / compiler specs? Visual Studio的突出显示似乎支持它,但是根据语言/编译器规范,它真的有效吗?
The This MSDN page gives the following note: 此MSDN页面提供以下注释:
The #define directive cannot be used to declare constant values as is typically done in C and C++. #define指令不能像在C和C ++中那样用于声明常量值。 Constants in C# are best defined as static members of a class or struct. C#中的常数最好定义为类或结构的静态成员。 If you have several such constants, consider creating a separate "Constants" class to hold them. 如果您有几个这样的常量,请考虑创建一个单独的“常量”类来保存它们。
I need this because using #if !SILVERLIGHT && !__ANDROID__ && !__IOS__
multiple times is difficult to manage. 我需#if !SILVERLIGHT && !__ANDROID__ && !__IOS__
是因为多次使用#if !SILVERLIGHT && !__ANDROID__ && !__IOS__
很难管理。
Of course we could also add SupportsMutex
to the "conditional compilation symbols" of a project, but this is more difficult to manage and less transparant. 当然,我们也可以将SupportsMutex
添加到项目的“条件编译符号”中,但这更难管理且透明度较低。
Yes. 是。 Looking at the C# specification a particular example of this usage is given in section 2.5.3 Declaration directives and deemed as valid: 查看C#规范 ,在2.5.3节声明指令中给出了这种用法的一个特定示例,该示例被视为有效:
#define Enterprise
#if Professional || Enterprise
#define Advanced
#endif
namespace Megacorp.Data
{
#if Advanced
class PivotTable {...}
#endif
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.