C# preprocessor directives
# Preprocessor directives instruct the compiler to preprocess information before actual compilation begins.
All preprocessor directives start with #. And on a line, only whitespace characters can appear before preprocessor directives. Preprocessor directives are not statements, so they do not end with a semicolon (;).
C# The compiler does not have a separate preprocessor, but instructions are processed as if there was a separate preprocessor. In C#, preprocessor directives are used to function in conditional compilation. Unlike C and C++ directives, they are not used to create macros. A preprocessor directive must be the only directive on the line.
C# List of preprocessor directives
The following table lists the preprocessor directives available in C#:
Preprocessor Directive | Description |
---|---|
#define | It is used to define a series of characters that become symbols. |
#undef | It is used to undefine a symbol. |
#if | It is used to test whether the symbol is true. |
#else | It is used to create compound conditional instructions, used together with #if. |
#elif | It is used to create compound conditional instructions. |
#endif | Specifies the end of a conditional instruction. |
#line | It lets you modify the number of lines the compiler uses and (optionally) the filenames where errors and warnings are output. |
#error | It allows generating an error from a specified location in the code. |
#warning | It allows generating a first-level warning from a specified location in the code. |
#region | It allows you to specify an expandable or collapsed code block when using the Outline feature of Visual Studio Code Editor. |
#endregion | It marks the end of the #region block. |
#define Preprocessor
#define preprocessor directive creates symbolic constants.
#define allows you to define a symbol such that, by using the symbol as an expression passed to a #if directive, the expression will return true. Its syntax is as follows:
#define symbol
The following program illustrates this:
#define PI using System; namespace PreprocessorDAppl { class Program { static void Main(string[] args) { #if (PI) Console.WriteLine("PI is defined"); #else Console.WriteLine("PI is not defined"); #endif Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following results:
PI is defined
Conditional Directives
You can use the #if directive to create a conditional directive. Conditional instructions are used to test whether a symbol is true. If true, the compiler executes the code between #if and the next instruction.
The syntax of conditional instructions:
#if symbol [operator symbol]...
Where, symbol is the name of the symbol to be tested. You can also use true and false, or place the negation operator before the symbol.
Operator symbols are operators used to evaluate symbols. The can operator can be one of the following operators:
== (equality)
!= (inequality)
&& (and)
- ##|| (or)
#if directive must explicitly end with an #endif directive.
The following program demonstrates the use of conditional directives:#define DEBUG #define VC_V10 using System; public class TestClass { public static void Main() { #if (DEBUG && !VC_V10) Console.WriteLine("DEBUG is defined"); #elif (!DEBUG && VC_V10) Console.WriteLine("VC_V10 is defined"); #elif (DEBUG && VC_V10) Console.WriteLine("DEBUG and VC_V10 are defined"); #else Console.WriteLine("DEBUG and VC_V10 are not defined"); #endif Console.ReadKey(); } }When the above code is compiled and executed, it produces the following results:
DEBUG and VC_V10 are defined