If the role of program flow control, will be directly compiled, executed.
#if是对编译器的指令, the function is to tell the compiler that some statement lines want to compile when the condition is met.
--------------------------------------------------------------------------------------------------------------- -----
#if allows you to start conditional directives, test one or more symbols to see if they evaluate to true. If they do evaluate to true, the compiler calculates all the code that is between the #if and the nearest #endif instruction. For example
Copy
#define DEBUG//. #if Debug Console.WriteLine ("Debug Version"); #endif
You can use operators = = (equal),! = (unequal), && (and), and | | (or) to calculate multiple symbols. You can also group symbols and operators in parentheses.
Notes
Using #if and #else, #elif, #endif, #define, and #undef directives, you can include or exclude code based on conditions that consist of one or more symbols. This is most useful when compiling a debug version of code or compiling a specific configuration.
Conditional directives that begin with a #if instruction must be explicitly terminated with a #endif instruction.
#define allows you to define a symbol that evaluates to trueby using the symbol as an expression passed to the #if instruction.
You can also use the/define compiler option to define symbols. You can use #undef to cancel the definition of a symbol.
Symbols defined with /define or #define do not conflict with variables that have the same name. That is, the variable name should not be passed to the preprocessor directive, and symbols can only be computed with preprocessor directives.
The range of symbols created with #define is the file in which the symbol is defined.
Example Copy
Preprocessor_if.cs#define debug#define vc_v7using System;public class MyClass { static void Main () {#if ( DEBUG &&! VC_V7) Console.WriteLine ("DEBUG is defined"); #elif (! Debug && vc_v7) Console.WriteLine ("VC_V7 is defined"), #elif (Debug && vc_v7) Console.WriteLine ("Debug and VC_V7 are defined"), #else Console.WriteLine ("Debug and VC_V7 is not defined"); #endif c6/>}}
Output
DEBUG and VC_V7 are defined
C # Differences in if and # if