The Conditional compilation command is used to include or exclude certain parts of the source file based on conditions.
According to the syntax, the Conditional compilation command must be written in the form of a set. The composition of the set is:#ifCommand, one or more#elifCommand (or none), one or more#elseCommand (or none) and one#endifCommand. Commands are the source code condition section. Each code is directly controlled by the command at the front of it. The condition section itself can contain nested condition compilation instructions, provided that these commands constitute a complete instruction set.
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 ());
10 # endif
11 # endif
12 commithelper ();
13}
14}
# Define or # UNDEF must be defined before all using namespaces. # define type is Boolean true, # UNDEF is Boolean false.
C # a better choice is proposed for this purpose: the conditional feature. Using the conditional feature, we can isolate some functions so that they can only take effect after defining certain environment variables or setting a certain value. The most common feature of conditional is to adapt the Code to a debugging statement .. . NET Framework has provided support for related functions. The following code demonstrates the working principle of the conditional feature and its applicability.
1 [conditional ("debug")]
2 Private void checkstate ()
3 {
4 // The Code remains unchanged. Www.elivn.com
5}
6
7
Namespace of conditional: using system. diagnostics;
The conditional method must be of the void return type.