Conditional compilation directives are used to include or exclude portions of a source file by criteria.
According to the syntax, conditional compilation directives must be written in the form of a set consisting of one #if
instruction, one or more #elif
instructions (or none), one or more instructions #else
(or none), and one #endif
instruction. Between directives is the conditional section of the source code. Each section of code is directly in the control of the command that precedes it. The conditional section itself can contain nested conditional compilation directives, provided that these directives form the complete set of instructions.
1 #define DEBUG//debugging on
2 #undef Trace//tracing off
3 Class Purchasetransaction
4 {
5 void Commit () {
6 #if Debug
7 checkconsistency ();
8 #if Trace
9 WriteToLog (this. ToString ());
Ten #endif
#endif
Commithelper ();
13}
14}
#define或 #undef must be defined in front of all using namespaces. #define类型为布尔值的真, #undef为布尔值的假.
C # has made a better choice for this: the conditional feature. Using the conditional feature, we can isolate some functions so that they only work if certain environment variables are defined or a value is set. The most common place to conditional features is to adapt the code to debug statements. NET Framework has provided the relevant functional support for this purpose. The following code shows how the conditional feature works and where applicable.
1 [Conditional ("DEBUG")]
2 private void CheckState ()
3 {
4//The code remains unchanged.
5}
6
7
Conditional namespace: Using System.Diagnostics;
The function of the conditional method must be a void return type.
This allows multiple versions of the program to be generated during compilation.
"Conditional Compilation"