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 (;).
The C# compiler does not have a separate preprocessor, however, 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# preprocessor directive list
The following table lists the preprocessor directives available in C#:
Preprocessor directive
Description
#define It is used to define a series of symbols that become symbols character.
#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 allows you to 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 level one 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)
You can also use parentheses to combine symbols with operators Make groups. Conditional directives are used to compile code in a debug build or when compiling a specified configuration. A conditional directive that begins with an #if directive must explicitly terminate 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 will produce the following results:
DEBUG and VC_V10 are defined
The above is the [c# tutorial] C# preprocessor directive Content, for more related content, please pay attention to the PHP Chinese website (www.php.cn)!