Preprocessing instruction and c preprocessing instruction
C # basic concepts of preprocessing commands
Although it is the same as the name of the term "preprocessing instruction" in C and C ++, C # does not actually have a separate preprocessing step, that is, there is no pre-processing compiler in environments such as.
The Preprocessing command can be used to replace the information in the source file. It must start with the # (Sharp) character and the name of the preprocessing command. For example:
#define MAX
We are used to set all names of preprocessing commands to uppercase. Unlike C and C ++, the name of the preprocessing instruction in C # is not followed by a number. The macro functions supported by C \ C ++ are not supported in C.
You can also use # region to set a region, or use Visual Studio to collapse the code. A string with the region name added after # region can be used to annotate the region.
# Region C # preprocessing command # endregion
Five pre-processing commands
1. # undef
We used # define above, but there are still # undef. So what does it do?
# Define MAXstatic void Main (string [] args) {# if (MAX) Console. WriteLine ("MAX has been defined. "); # Endif}
If we Debug the program, we will see the following in the console:
MAX has been defined.
If we Release the program, the results will be the same, but if we add the following at the beginning of the Code:
#undef MAX
Then, the Debug console will not receive any message.
2. # if, # else, # elif
If you want the console to display a situation where MAX is not defined, use # else To match # if.
# If (MAX) Console. WriteLine ("MAX has been defined. "); # Else Console. WriteLine (" MAX is not defined yet. "); # Endif
Maybe you also saw # elif when typing # else. This is the corresponding else if. Their logic is the same as that of the general if set.
# Define MAX # undef MAX # define MINstatic void Main (string [] args) {# if (MAX) Console. WriteLine ("MAX has been defined. "); # Elif (MIN) Console. WriteLine (" MIN has been defined. "); # Else Console. WriteLine (" MAX is not defined yet. "); # Endif Console. ReadLine ();}
3. # warning, # error
These two preprocessing commands are used to throw exceptions and errors.
# Warning: This is a forced exception.
# Error: This is a force-throw error.
4. # line
We can also use # line to set the number of lines in the following # error line code to 10000, and set the file name to "error".
Static void Main (string [] args) {# line 10000 "error" # error is a force-throw error. }
If we add such a line of code, the row number and file name will be restored to the default one.
# Line 10000 "Error" # error is a force-throw Error. # Line default # warning is a forced warning.
5. # pragma
Add this line of code before the Program class, which will cause the CS3021 error.
[CLSCompliant(false)]
Then we add the following line of code before the above line of code to disable warning 3021. Then Debug again and you will find that the original error is no longer there.
#pragma warning disable 3021
If you want to restore the CS3021 error in the following text, use restore.
#pragma warning restore 3021
The complete code is as follows.
#pragma warning disable 3021 [CLSCompliant(false)] class Program { static void Main(string[] args) {} }#pragma warning restore 3021 [CLSCompliant(false)] public class OtherProgram { public static void OtherMain() {} }
This pre-processing command is actually very complicated and has been introduced on major encyclopedias. The following is from Wikipedia. Portal: pragma once.