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

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: