Search code examples
c#compiler-directives

#Define Compiler Directive in C#


In C, I could declare a compiler directive as follows:

#define MY_NUMBER 10

However, in C#, I only appear to be able to do this:

#define MY_NUMBER

Which is obviously useless in this case.

Is this correct, or am I doing something wrong? If not, can anyone suggest a way of doing this, either at namespace or solution level? I thought of maybe creating a static class, but that seems to be overkill for one value.


Solution

  • Yes, it is correct.

    Here's a quote from the MSDN documentation:

    The pre-processing directives provide the ability to conditionally skip sections of source files, to report error and warning conditions, and to delineate distinct regions of source code. The term "pre-processing directives" is used only for consistency with the C and C++ programming languages. In C#, there is no separate pre-processing step; pre-processing directives are processed as part of the lexical analysis phase.

    So you can't really define compiler constants, like in C and C++.

    Related resources: