C # Preprocessor directives do not translate to executable code commands, but affect all aspects of the compilation process.
For example, when you plan to release two versions of the code. That is, the basic version and the Enterprise Edition with more versions, you can use the preprocessor directives,
When compiling the basic version, using preprocessor directives prevents the compiler from compiling code that is related to additional functionality.
You can also use preprocessor directives when writing code that provides debugging information
There are not many pre-processing instructions, so let's give a simple example, specific explanations and syntax, which can be found in the MSDN Help documentation
First, write the following code
usingSystem;namespaceconsoleapplication{classProgram {Static voidMain () {Console.WriteLine ("Debug State"); Console.WriteLine ("Tracking Status"); Console.WriteLine ("Running State"); Console.WriteLine ("Please enter a number:"); varnum =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); } }}
The post-operation effect is as follows:
If you use preprocessor directives, add instructions at the top of the code
#define DEBUG#define TRACE
Then, add the judgment at the location where you debug and track
#defineDEBUG#defineTRACEusingSystem;namespaceconsoleapplication{classProgram {Static voidMain () {#ifDEBUGConsole.WriteLine ("Debug State");#elifTRACEConsole.WriteLine ("Tracking Status");#elseConsole.WriteLine ("Running State"); Console.WriteLine ("Please enter a number:"); varnum =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); #endif } }}
At this point, only the debug state of the code can be executed normally, if you use #undef to cancel the definition, then you can specify the code snippet to run, as
#defineDEBUG#defineTRACE#undefDEBUG#undefTRACEusingSystem;namespaceconsoleapplication{classProgram {Static voidMain () {#ifDEBUGConsole.WriteLine ("Debug State");#elifTRACEConsole.WriteLine ("Tracking Status");#elseConsole.WriteLine ("Running State"); Console.WriteLine ("Please enter a number:"); varnum =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); #endif } }}
You can also display specified warning and error messages by #warning or #error directives
#warning var num = convert.toint32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}"
Where warnings can be compiled, and errors cannot be compiled by normal, the effect is as follows:
You can get the number of the warning in the Output window
Suppress current warnings or errors by #pragma in code
#pragma warning Disable 1030
Finally, it is often used, using #region to achieve Code area segmentation
#region Here is the code area Console.WriteLine (" running state "); Console.WriteLine (" Please enter a number:"); #warning must add a defensive code here var num = convert.toint32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); #endregion
The complete program code is as follows:
#defineDEBUG#defineTRACE#undefDEBUG#undefTRACE#pragmaWarning Disable 1030usingSystem;namespaceconsoleapplication{classProgram {Static voidMain () {#ifDEBUGConsole.WriteLine ("Debug State");#elifTRACEConsole.WriteLine ("Tracking Status");#else #regionThis is the code area.Console.WriteLine ("Running State"); Console.WriteLine ("Please enter a number:");#warningThe defensive code must be added here.varnum =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("C # Basic syntax: The number you entered is: {0}", num); #endregion#endif } }}
C # Preprocessor Directives