1. # define and # undef
Usage:
# Define DEBUG
# Undef DEBUG
# Define tells the compiler that I define a DEBUG symbol. It is similar to a variable, but it does not have a specific value. It can be regarded as a symbol. # Undef is the definition of this symbol. If the DEBUG symbol is not defined, # undef does not work; otherwise, # define does not work. Both must be placed before the source code. The order of the two depends on the Code:
# Define DEBUG
# Undef DEBUG
In this case, DEBUG is not defined. If the two are in another order, the compiler considers DEBUG to be defined.
2. # if, # elif, # else, # endif
This tells the compiler to control the process of compiling code. Consider the following code:
# If DEBUG
Console. Write ("debug ");
# Elif RELEASE
Console. Write ("realse ");
# Else
Console. Write ("other ");
# Endif
The above code means that if DEBUG is defined, debug is output, and RELEASE is defined, realse is output; otherwise, other is output. What if DEBUG and REALSE are defined? You can give it a try.
3. # warning and # error
The two parameters can tell the compiler whether a warning or error message is returned. Compilation stops after the error message is exceeded.
Refer to the following code
# If DEBUG ------> if the current DEBUG status is selected as DEBUG, the default value is # define.
# Warning "the current status is Ddbug"
# Elif RELEASE ------> If the release status is selected, there is no default value for this. I do not know if my test is correct.
# Warning "the current status is Realse"
# Else
# Error "and identify the status"
# Endif
4. # region and # endregion
These two are used to form a code block.
5. # line (the specified use is not clear. Refer to MSDN)
This command can change the file name and line number information displayed by the compiler in the warning and error messages, and use # line default to restore the row number to the default line number.
The following example shows how to report two warnings associated with the row number. # Line 200 Command forces the row number to be 200 (although the default value is #7 ). The result of another line (#9) as the default # line command follows the normal sequence.
// Preprocessor_line.cs
Public class MyClass2
{
Public static void Main ()
{
# Line 200
Inti; // CS0168 on line 200
# Line default
Char c; // CS0168 on line 9
}
}
Example 2
The following example shows how the debugger ignores hidden rows in the code. When running this example, it displays three lines of text. However, when you set the breakpoint as shown in the example and press the F10 key to pass the code one by one, you will see that the debugger ignores hidden lines. Note that the debugger ignores the breakpoint even if it is set on the hidden line.
// Preprocessor_linehidden.cs
Using System;
Class MyClass
{
Public static void Main ()
{
Console. WriteLine ("Normal line #1."); // Set a break point here.
# Line hidden
Console. WriteLine ("Hidden line .");
# Line default
Console. WriteLine ("Normal line #2 .");
}
}