简体   繁体   English

C#可以使用#if来比较小于或大于的值吗?

[英]Can C# use #if to compare a value with less than or greater than?

Is there any way in C# to use the #if directive to compare a value with a less than or greater than operator? 在C#中是否有任何方法可以使用#if指令将值与小于或大于运算符进行比较?

Example: 例:

#if GAME_SDK_VERSION < 500

#else

#endif

EDIT I see the duplicate question link but it does not clearly address my simple question of using greater than or less than symbols. 编辑我看到重复的问题链接,但它没有清楚地解决我使用大于或小于符号的简单问题。 I think this question is valuable enough, along with the answer given to stand on it's own. 我认为这个问题很有价值,同时也提供了自己的答案。

No, you can't. 不,你不能。 Quote from the docs : 文档引用:

Unlike C and C++, you cannot assign a numeric value to a symbol; 与C和C ++不同,您不能为符号分配数值; the #if statement in C# is Boolean and only tests whether the symbol has been defined or not. C#中的#if语句是布尔值,仅测试符号是否已定义。

As a workaround I have used symbols that mean "greater than or equal to X", for instance for version 3 I would define something to the effect of VER_GTE_1 , VER_GTE_2 and VER_GTE_3 . 作为一种解决方法,我使用了“大于或等于X”的符号,例如对于版本3,我将定义VER_GTE_1VER_GTE_2VER_GTE_3的效果。 The other alternative is to only define the current version and use a long list of || 另一种方法是仅定义当前版本并使用长列表|| in the #if . #if

This does quickly get infeasible for very large numbers. 对于非常大的数字,这很快就会变得不可行。 Perhaps the best way is to avoid preprocessor directives as much as possible - I think C# has better opportunities for this than C/C++. 也许最好的方法是尽可能避免预处理程序指令 - 我认为C#比C / C ++有更好的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM