#define in C#

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
 
M

Marcel Müller

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
 
J

Jeff Johnson

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

Alain Dekker

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top