Guide
1. What is preprocessing
2. Benefits of Pretreatment
3. Common preprocessing directives in C #
4. Summary
What is preprocessing
In computer science, preprocessing usually refers to the process of using a program (usually a preprocessor) to replace a source code (such as a. cs C # Source) in a certain format according to some rules. This process occurs in the lexical analysis phase, and the source files are still available. For compiling, it can be divided into three stages, lexical analysis, syntax analysis, executable program generation.
Above is my own understanding, may not be accurate. PS I have been in the Zhong Ke two semesters of the compiling principle class, but all because of some reasons for work overtime and so not a semester is to adhere to complete listening, really ashamed
Benefits of preprocessing
What good is this, I haven't summed it up for the time being. Fill in later
Common pre-processing directives in C #
Common preprocessing directives in C # can be divided into 4 categories:
1, preprocessing symbol definition Directive #define and #undef
2. Conditional compilation instruction #if #elif #else #endif
3. Diagnostic Instructions #error and #warning
4. region directive #region #endregion
Now one by one description
Preprocessing definition Symbols
We can use #define to define preprocessing symbols, to cancel the definition of preprocessing symbols with #undef, although the compiler provides such a mechanism, but I do not recommend that the preprocessing symbol definitions be written in the source code because of the lack of flexibility. The C # compiler provides a command to place the definition of a preprocessing symbol in a command-line argument csc/d:sybolname: It is recommended to define preprocessing symbols in the form of compiler command parameters
Conditional compilation directives
#if #elif #else #endif and C # syntax If-elseif else semantically similar, here is not much to say. It supports && | | ! Login Logic Combination
Let's look at an example Demo.cs
//#define DEBUG_MODE1//defining preprocessing Symbols Debug_mode1//#undef debug_mode1//To cancel the definition of a preprocessing symbol Debug_mode1usingSystem;classapp{Static voidMain () {#if(Debug_mode1 &&!) DEBUG_MODE2)//Open Debug_mode1 onlyDebug_mode1 (); #elif(! Debug_mode1 && Debug_mode2)//Open Debug_mode2 onlyDebug_mode2 (); #elif(Debug_mode1 && Debug_mode2)//Open Debug_mode1 and Debug_mode2 at the same timeDebug_mode1 (); Debug_mode2 (); #elseConsole.WriteLine ("normal Operation"); #endif } #if(DEBUG_MODE1)Static voidDebug_mode1 () {Console.WriteLine ("turn on Debug_mode1 mode"); } #endif #if(DEBUG_MODE2)Static voidDebug_mode2 () {Console.WriteLine ("turn on Debug_mode2 mode"); } #endif}
The compilation commands were
CSC Demo.cs
Csc/d:debug_mode1 Demo.cs
Csc/d:debug_mode2 Demo.cs
Csc/d:debug_mode1,debug_mode2 Demo.cs
The operating effects were
Diagnostic instructions
Format
#error message
#waming message
Message can be any format text, such as Chinese
See Code DiaDemo.cs
usingSystem;#warningBefore compiling the code, you must first git code, such as git code to ignoreclassapp{Static voidMain () {Debuginvoke (); #if! Debug_modeConsole.WriteLine ("normal Operation"); #endif } Static voidDebuginvoke () {#if! Debug_mode#errorThis method can only be used in debug mode#endif Console.WriteLine ("now enter debug mode ..."); }}
The compilation commands were
CSC DiaDemo.cs
Csc/d:debug_mode DiaDemo.cs
The effects were
Region directive
This command I want to use C # should be familiar with, omit not the table
Summarize
1. Any #define and #undef instructions in the source file must appear in front of a C # identifier in the source file, or a compile-time error will occur
2. We do not recommend defining precompiled symbols in the source code in # define mode, we recommend that you define precompiled symbols in the way of compiler command line arguments.
3. Preprocessing symbol definitions are specified at compile time that are not specified at runtime
See an example of a run-time specified run parameter Demo2.cs
using System; class app{ staticvoid Main (string[] args) { foreach (string in args) { Console.WriteLine (arg); }}}
Compile Command csc demo2.cs
Run effect
End of this article
C # preprocessing Directives