4

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.

Tyress
  • 3,573
  • 2
  • 22
  • 45
jjxtra
  • 20,415
  • 16
  • 100
  • 140
  • 1
    No, you can only define compile time symbols. They have no inherent value. – Jeff Mercado Mar 07 '16 at 23:56
  • 1
    Possible duplicate of [Compile time checking of conditional compilation symbols in C# (see example)?](http://stackoverflow.com/questions/3806738/compile-time-checking-of-conditional-compilation-symbols-in-c-sharp-see-example), also http://stackoverflow.com/questions/3842590/how-do-i-set-a-conditional-compile-variable – l'L'l Mar 08 '16 at 00:00

1 Answers1

3

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++.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • Thanks. Right now I am using #if with || for Unity versions, which gets large, especially with API changes between versions like 4.2 and 4.3, etc. – jjxtra Mar 08 '16 at 03:06