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.
Preprocessor directive | Usage syntax | Comments |
---|---|---|
#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 text | Used to show a warning or error message during compilation |
#error | #error text | |
#region | #region name | Used to mark and unmark a region of code |
#endregion | endregion | |
#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.
3.148.102.164