C# Preprocessor directives
Preprocessor directives instruct the compiler to preprocess information before actual compilation begins.
Through these directives, you can control how the compiler compiles files or which parts to compile. Common preprocessor directives include conditional compilation, macro definitions, and so on.
All preprocessor directives begin with#They begin with #, and on one line, only whitespace characters can appear before a preprocessor directive.
Preprocessor directives are not statements, so they do not end with a semicolon.;end.
The C# compiler does not have a separate preprocessor; however, the directives are handled as if there were one. In C#, preprocessor directives are used for conditional compilation. Unlike C and C++, they are not used to create macros. A preprocessor directive must be the only directive on that line.
C# Preprocessor Directive List
The following table lists the preprocessor directives available in C#:
| Directive | Description |
|---|---|
#define | Define a symbol that can be used for conditional compilation. |
#undef | Undefine a symbol. |
#if | Begin a conditional compilation block; include the code block if the symbol is defined. |
#elif | If the previous#ifor#elifIf the previous condition is not satisfied and the current condition is satisfied, include the code block. |
#else | If the previous#ifor#elifIf the condition is not satisfied, include the code block. |
#endif | End a conditional compilation block. |
#warning | Generate a compiler warning message. |
#error | Generate a compiler error message. |
#region | Mark a code region that can be collapsed and expanded in the IDE, making code organization and reading easier. |
#endregion | End a code region. |
#line | Change the line number and file name in compiler output, which can be used for debugging or for code generated by tools. |
#pragma | Used to send special instructions to the compiler, such as disabling or restoring specific warnings. |
#nullable | Control the nullable context and annotations, allowing compiler checks for nullable reference types to be enabled or disabled. |
Example
#if DEBUG
Console.WriteLine("Debug mode");
#elif RELEASE
Console.WriteLine("Release mode");
#else
Console.WriteLine("Other mode");
#endif
#warning This is a warning message
#error This is an error message
#region MyRegion
// Your code here
#endregion
#line 100 "MyFile.cs"
// The next line will be reported as line 100 in MyFile.cs
Console.WriteLine("This is line 100");
#line default
// Line numbering returns to normal
#pragma warning disable 414
private int unusedVariable;
#pragma warning restore 414
#nullable enable
string? nullableString = null;
#nullable disable
#define and #undef Preprocessor
#defineUsed to define a symbol (usually for conditional compilation),#undefUsed to undefine a symbol.
#define DEBUG #undef RELEASE
#define allows you to define a symbol so that, by using the symbol as an expression passed to the #if directive, the expression returns true. Its syntax is as follows:
#define symbol
The following program illustrates this:
Example
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: #if, #elif, #else, and #endif
You can use#ifDirective to create a conditional directive.
Conditional directives are used to test whether a symbol is true. If true, the compiler will execute#ifCode between this directive and the next one.
Syntax of conditional directives:
#if symbol [operator symbol]...
where,symbolthe name of the symbol to test. You can also use true and false, or place a negation operator before the symbol.
Common operators include:
- == (equals)
- != (not equal to)
- && (AND)
- || (OR)
You can also use parentheses to group symbols and operators. Conditional directives are used to compile code in debug builds or when compiling a specified configuration. A conditional directive that begins with a#ifdirective must explicitly end with a#endifInstruction terminated.
#define DEBUG
#if DEBUG
Console.WriteLine("Debug mode");
#elif RELEASE
Console.WriteLine("Release mode");
#else
Console.WriteLine("Other mode");
#endif
The following program demonstrates the use of conditional directives:
Example
#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
#warning and #error
#warning is used to generate compiler warnings, and #error is used to generate compiler errors.
#warning This is a warning message #error This is an error message
#region and #endregion
Used for code folding, making code more readable.
#region MyRegion
// Your code here
#endregion
#line
Used to change the file line number and file name in compiler output.
#line 100 "MyFile.cs"
// The next line will be reported as line 100 in MyFile.cs
Console.WriteLine("This is line 100");
#line default
// Line numbering returns to normal
#pragma
Used to send special instructions to the compiler. The most common use is to disable specific warnings.
#pragma warning disable 414
private int unusedVariable;
#pragma warning restore 414
Notes on using preprocessor directives
- Improve code readability: Use
#regionCan help separate code blocks and improve code organization. - Conditional Compilation: through
#ifSuch directives can compile different code in development and production environments, making debugging and release easier. - Warnings and errors: through
#warningand#errorThey can prompt developers to pay attention to specific issues at compile time.
By correctly using these preprocessor directives, you can better control the compilation process of code and improve code flexibility and maintainability.
other extensions