Chapter 24. C# preprocessor directives

If you have ever read C/C++ codes before, you can't have missed those strange looking #define and #include statements found at the beginning of the source codes. These special keywords, which begin with a # sign, are called preprocessor directives.

Preprocessor directives, though part of the source codes, are not compiled into IL codes. They are only used by the C# compiler for making certain decisions during compilation. This will become clear as you go through the examples which follow.

Table 24.1 shows the preprocessor directives supported in C#: [1]

[1] C/C++ developers: there have been significant changes to the C/C++ preprocessor directives in C#. Notice that #include is no longer a valid directive and #ifdef has been replaced with #if. #define cannot be used to define constants or write macro functions. #pragma has been also removed.

Table 24.1. Valid preprocessor directives in C#
Preprocessor directiveUsage syntaxComments
#define#define <symbol>Used to define and undefine a symbol
#undef#undef <symbol>
#if#if <symbol> [operator <symbol>] ...Used for conditional compilation
#else#else
#elif#elif <symbol> [operator <symbol>] ...
#endif#endif
#warning#warning textUsed to show a warning or error message during compilation
#error#error text
#region#region nameUsed to mark and unmark a region of code
#endregionendregion
#line [1]line [number ["file_name"] | default]Used to modify the compiler's line number and file name output for errors and warnings

[1] The #line preprocessor directive is seldom used, and will not be discussed in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.102.164