Home > Article > Backend Development > What are preprocessor directives in C#?
The C# compiler does not have a separate preprocessor; however, these directives are processed as if there were one. In C#, preprocessor directives are used to aid conditional compilation.
Preprocessor directives issue instructions to the compiler to preprocess information before actual compilation begins.
The following are the preprocessor directives in C#-
Sr.No. | Preprocessor Directives & Description |
---|---|
1 |
#define It defines a sequence of characters, called a symbol. |
2 |
#undef It allows you to undefine symbols. |
3 |
#if It allows testing one or more symbols to see them Whether the calculation result is true. |
4 |
#else It allows creating compound conditional directives together with #if. |
5 |
#elif It allows the creation of compound conditional instructions. |
#endif Specifies the end of the conditional directive. |
7 |
#line It allows you to modify the compiler's line numbers as well as ( Optional) Filename output for errors and warnings. |
8 |
#error It allows generating errors coming from specific locations in the code. |
9 |
#warning It allows to generate one level from a specific location in the code warn. |
10 |
#region It allows you to specify when using the Visual Studio Code Editor The outline feature allows you to expand or collapse blocks of code. |
11 |
#endregion It marks the end of the #region block. |
Let’s see an example to understand the usage of preprocessor directives in C# -
#define PI using System; namespace Demo { class Program { static void Main(string[] args) { #if (PI) Console.WriteLine("PI is defined"); #else Console.WriteLine("PI is not defined"); #endif Console.ReadKey(); } } }
The above is the detailed content of What are preprocessor directives in C#?. For more information, please follow other related articles on the PHP Chinese website!