Preprocessor directives are a block of statements that gets processed before the actual compilation starts. C# preprocessor directives are the commands for the compiler that affects the compilation process.
These commands specifies which sections of the code to compile or how to handle specific errors and warnings.
C# preprocessor directive begins with a # (hash) symbol and all preprocessor directives last for one line. Preprocessor directives are terminated by new line rather than semicolon.
#if
Checks if a preprocessor expression is true or not
#if preprocessor-expression
code to compile
#endif
#elif
Used along with #if to check multiple preprocessor expressions
#if preprocessor-expression-1
code to compile
#elif preprocessor-expression-2
code to compile
#endif
#else
Used along with #if to create compound conditional directive.
#if preprocessor-expression
code to compile
#elif
code to compile
#endif
#endif
Used along with #if to indicate the end of a conditional directive
#if preprocessor-expression
code to compile
#endif
#define
Used to define a symbol
#define SYMBOL
#undef
Used to undefine a symbol
#undef SYMBOL
#warning
Allows us to generate level 1 warning from our code
#warning warning-message
#error
Allows us to generate error from our code
#error error-message
#line
Allows us to modify the compiler's line number and filename to display errors and warnings
#line line-number file-name
#region
Allows us to create a region that can be expanded or collapsed when using a Visual Studio Code Editor
#region region-description
codes
#endregion
#endregion
Indicates the end of a region
#region region-description
codes
#endregion