Preprocessing instructions
These instructions/commands are not converted into executable code, but will affect all aspects of the compilation process; for example, the compiler can not compile a certain part of the code, etc.
The main preprocessing instructions in C#
#define and #undef
#define directive definition:
#define DEBUG
It tells the compiler that the symbol DEBUG exists; this symbol is not part of the actual code, but may be compiled according to this symbol when the compiler compiles the code.
#undef definition:
#undef DEBUG
Used to remove the defined symbol DEBUG. If such a tag does not exist, the #undef directive will not take effect. Similarly, there will be no change in defining a tag with the same name with #define again.
Notice:
- You need to write the #define and #undef directives where they are before the actual business code begins.
- #define itself is useless and needs to be used in combination with other preprocessor instructions; for example #if
#if, #elif, #else and #endif
These instructions tell the compiler whether to compile the block of code contained in it. For example:
int DoSomeWork(double x) { // do something #if DEBUG ($"x is {x}"); #endif }
The statements in this code will only be truly compiled when compiling after defining the symbol DEBUG with the #define instruction;
If the compiler does not find the DEBUG symbol, this code will be ignored during compilation.
The #elif(= else if) and #else directives can be used in the #if block:
#define ENTERPRISE #define W10 // further on in the file #if ENTERPRISE // do something #if W10 // some code that is only relevant to enterprise // edition running on W10 #endif #elif PROFESSIONAL // do something else #else // code for the leaner version #endif
#if and #elif also support some limited logical operators, which you can use using !, ==, != and || etc.
A tag is considered true if it exists, and false if it is not defined, so you can use it like this:
#if W10 && (ENTERPRISE==false) // if W10 is defined but ENTERPRISE isn't
#warning and #error
When the compiler encounters #warning, a warning message will be generated;
When the compiler encounters #error, an error message will be generated;
class Program { static void Main(string[] args) { #warning this is a warning message which will be shown when compile ("Hello World!"); #error this is a error message, and will break build } }
Compilation result:
(10,10): warning CS1030: #warning: 'this is a warning message which will be shown when compile' [/define_warning/define_warning.csproj]
(14,8): error CS1029: #error: 'this is a error message, and will break build' [/define_warning/define_warning.csproj]
1 Warning(s)
1 Error(s)
Use these directives to check if the #define statement is doing something wrong, and use #warning to remind you to do something:
#if DEBUG && RELEASE #error "You've defined DEBUG and RELEASE simultaneously!" #endif #warning "Don't forget to remove this line before the boss tests the code!" ("*I love this job.*");
#region and #endregion
It can be used to identify a piece of code, which is more useful in Visual Studio or other IDEs that can be recognized.
#region Member Field Declarations int x; double d; Currency balance; #endregion
#line
The #line directive can be used to change the corresponding file name and line number information when the compiler outputs warnings and errors. In fact, this may be used less.
Mainly when using third-party packages, sometimes the line number or file name reported by the compiler does not match the actual situation.
#line can be used to restore this match.
#line 164 "" // We happen to know this is line 164 in the file , // before the intermediate package mangles it. // later on #line default // restores default line numbering
#pragma
The #pragma directive can be used to terminate or restore a specified number to the compiler warning.
Unlike command-line options, the #pragma directive can be implemented at the class or method level.
For example:
class Program { static void Main(string[] args) { int i = 0; ("Hello World!"); } }
There will be warning when compiling:
(9,17): warning CS0219: The variable 'i' is assigned but its value is never used [/define_warning/define_warning.csproj]
1 Warning(s)
0 Error(s)
From the warning information, it can be seen that it is warning CS0219. After joining #pragma, there will be no warning.
#pragma warning disable CS0219 public class Program { static void Main(string[] args) { int i = 0; ("Hello World!"); } } #pragma warning restore CS0219
Note: The code of warning is case sensitive, CS2019 must be capitalized, but if written as CS2019, it will be useless.
The above is the detailed content of the analysis of C# preprocessing instructions. For more information about C# preprocessing instructions, please pay attention to my other related articles!