简体   繁体   中英

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?

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; the #if statement in C# is Boolean and only tests whether the symbol has been defined or not.

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 . The other alternative is to only define the current version and use a long list of || in the #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++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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