[转] Preprocessing instructions in C #, how much did you use?

xiaoxiao2021-03-05  24

The preprocessing instruction in the C # as a pair in the pre-processing: #REGION Name, # endregion may be the most used, I also use it to make code block, in a relatively long CS file, this is true It is a good way to make you clearet code. More people use #Region. There is also a few pairs of pretreatment instructions in C #, which may be used. #define symbol #undef symbol #if symbol [operator symbol2] ... #else #elif symbol [operator symbol2] #ENDIF #Warning text text refers to the warning text in the compiler output #Error text text refers to the compiler output Error message #line number [file]

Regarding #define, you want to explain the few points, # define debug is equivalent to do not write this sentence, you must not define it must be written on #undefin debug (C # Essentials, say #define debug is equivalent to #define debug true But this seems to have a problem)

This code can explain 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&v_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 is not defined "); #ENDIF}} We can use these instructions to automate the work of the test period and the connection string of the release period database. #if! Release constr = testsqlserverConnectionstring #Else constr = releasesqlserverConnectionstring #ENDIF Add a sentence with a #undef research when compiled code, then delete this sentence after publishing.

#define debug

Using system;

Namespace syntaxtest {public class class2 {public class2 () {#if debug #warning debug is defined #ENDIF}}} The above code will generate a warning when compiling.

#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 when compiling, the compilation is not passed, so we can use the above two instructions to make some self Defined compilation detection.

#LINE enables you to modify the line number of the compiler and (optional) error and warning file name output. #Line [Number ["File_Name"] | Default]

Number To specify the number specified for the row behind the source code file. "File_Name" (optional) wants to appear in the file name in the compiler output. By default, the actual name of the source code file is used. The file name must be enclosed in double quotes (""). The DEFAULT reset the line number in the file. Remark #Line may be used by automatic intermediate steps during the generation. For example, if the intermediate step is removed from the original source code file, you still want the compiler to generate an output based on the original line number in the file, then remove the row, then use #LINE to simulate the original line number.

Public class myclass2 {public static void main () {#Line 200 INT i; // This line is in 200 lines, but it is still 6 #line default char c; // this line is in 7 lines, but It is still 8} in VS.

转载请注明原文地址:https://www.9cbs.com/read-34512.html

New Post(0)