#501 – Differences Between #define in C++ and C#

In C++, you could use the #define preprocessor directive in three ways:

  • Define a symbol and then later check to see if the symbol exists
  • Define a symbol, giving it a particular value that you can later check
  • Define a symbol and give it a definition (a macro)
  • Provide parameters as part of the symbol’s definition

In C++, the #define directive assigns some arbitrary text to a symbol and the preprocessor substitutes the text for the symbol, wherever the symbol appears in your code.

As an example, you could use C++ to create a macro by defining the symbol MAX as follows:

#define MAX(a,b) (a>b)?a:b

The #define directive in C# is much more limited.  Of the four uses for #define listed above, only the first one is supported in C#.  You use the #define directive to define a symbol that is either present or not present.