You can use preprocessor directives in csharp to provide straightforward instructions to the compiler. For example, these directives allow you to execute certain code elements only under predefined conditions. Another possible field of use is simply the structuring of your source code into blocks, which can be folded in and out of Visual Studio.
In this article I will discuss some of these directives. Preprocessor directives are always started with a #-symbol.
#define & #undef
The first two directives I would like to mention are #define and #undef. They are used to define symbols and remove them. Imagine such a symbol as a simple variable, the next elements will help to illustrate it as well. Note, that #define-elements need to be at the very top of a file, even above the using-statements!
#if, #else, #elif & #else
The next symbols are #if, #else, #elif and #endif. As you have probably already guessed, they are used to execute certain source code only under predefined conditions. And this is exactly where the symbols I defined in the previous section play a role. In the following source code I have defined a symbol, which is then checked for existence. According to this definition, the output “Running in demomode!” will be performed. You can also use the predefined symbol “DEBUG”, to check wether your application runs in debug or release mode.
|
|