Preprocessing commands in C #
As a pair in Preprocessing: # region name, # endregion may be the most commonly used by everyone. I also use it to block code. In a long CS file, this is indeed a good way to make the code clearer. vs also uses this to automatically include the automatically generated code, by doing so, developers can view their code more clearly and enable a good header for more people to use # region.
C # contains several pairs of preprocessing commands, which may be rarely used.
# Define symbol
# UNDEF symbol
# If symbol [operator symbol2]...
# Else
# Elif symbol [operator symbol2]
# Endif
# Warning text refers to the warning text in the compiler output
# Error text indicates the error message in the compiler output.
# Line number
# Define debugging is equivalent to not writing this sentence. to define it, you must write # undefin debug
(In C # essentials, # define debug is equivalent to # define debug true, but this seems to be a problem)
This code can illustrate several commands:
# Define debug
# Define vc_v6
Using system;
Public class myclass
{
Public static void main ()
{
# If (debug &&! Vc_v6)
Console. writeline ("Debug is defined ");
# Elif (! Debug & vc_v6)
Console. writeline ("vc_v6 is defined ");
# Elif (debug & vc_v6)
Console. writeline ("Debug and vc_v6 are defined ");
# Else
Console. writeline ("Debug and vc_v6 are not defined ");
# Endif
}
}
We can use these commands to automatically handle tasks such as the connection strings of the database during the test period and the standby period.
# If! Release
Constr = testsqlserverconnectionstring
# Else
Constr = releasesqlserverconnectionstring
# Endif
Add # UNDEF resease to the Code, and delete this sentence after the code is published.
# Define debug
Using system;
Namespace syntaxtest
{
Public class class2
{
Public class2 ()
{
# If debug
# Warning debug is defined
# Endif
}
}
}
The above code generates a warning during compilation.
# Define debug
Using system;
Namespace syntaxtest
{
Public class class2
{
Public class2 ()
{
# If debug
# Error debug is defined
# Endif
}
}
}
The above code will generate an error during compilation and compilation fails. Therefore, we can use the above two commands to perform custom compilation detection.
# Line enables you to modify the compiler's row number and (optional) Output of file names for errors and warnings.
# Line [number ["file_name"] | default]
Number
The number specified for the row following the source code file.
"File_name" (optional)
Name of the file to appear in the compiler output. By default, the actual name of the source code file is used. The file name must be enclosed in double quotation marks.
Default
Resets the row numbers in the file.
Remarks
# Line may be used by automatic intermediate steps during generation. For example, if the intermediate step removes rows from the original source code file, but you still want the compiler to generate output based on the original line number in the file, you can remove the row and use # line to simulate the original line number.
Public class myclass2
{
Public static void main ()
{
# Line 200
Int I; // This row is in 200 rows, but in Vs, we can see 6 rows.
# Line default
Char C; // This row is in 7 rows, but what we see in vs is 8
}
}
Please criticize and correct.