#define in C#

  • Thread starter Thread starter Alain Dekker
  • Start date Start date
A

Alain Dekker

In C++ you can do this:
#define MY_MAGIC_NUMBER 5
....
if (nTheUserID == 5)
{
DoSomething();
}

but I can't find how to do that in C#. The MSDN documentation appears to
suggest that this is not possible like it was in C/C++. Is that right? Is
there any way to use the preprocessor to define macros in C#?

Thanks,
Alain
 
Alain said:
In C++ you can do this:
#define MY_MAGIC_NUMBER 5
...
if (nTheUserID == 5)
{
DoSomething();
}

but I can't find how to do that in C#. The MSDN documentation appears to
suggest that this is not possible like it was in C/C++. Is that right?

Yes that's right. And the use of #define for this purpose in C++ is an
abuse of the preprocessor anyway - with various side effects in case of
trivial names.

In C# as well as in C++ this should read:

const int MY_MAGIC_NUMBER = 5;

Is
there any way to use the preprocessor to define macros in C#?

No.


Marcel
 
In C++ you can do this:
#define MY_MAGIC_NUMBER 5
...
if (nTheUserID == 5)
{
DoSomething();
}

but I can't find how to do that in C#. The MSDN documentation appears to
suggest that this is not possible like it was in C/C++. Is that right? Is
there any way to use the preprocessor to define macros in C#?

No. #define in C# is basically a Boolean. Either a symbol is defined or it
isn't; it holds no value. And there are no preprocessor macros whatsoever.
 
OK, thanks to all. The preprocessor macro feature in C++ was powerful...but
also could be very hard to debug. Its ok that C# doesn't do it, but I guess
its ok to mourn this feature's departure for a few hours...

Alain
 
Back
Top