C # Preprocessor directives are called at compile time. The preprocessor directive (preprocessor directive) tells the C # compiler what code to compile and indicates how to handle specific errors and warnings. C # Preprocessor directives can also tell the C # Editor about the code organization. #define Debug#define Issay The above statement defines a precompiled symbol whose scope is the entire file where he is located, the statement that defines the symbol must appear before all code, or an exception will occur when compiling: cannot be after the first tag of the file. Defines or cancels the preprocessor symbol. We can also use the #undef to cancel the definition of a symbol, let's take a look at an example. Copy code # define DEBUG#UNDEF debug#define Issay using System; Namespace Justdoit {class Program {static void Main (string[] args) {#if DEBUG Console.Write ("Debug."); #endif #if issay console.write ("hello."); #else Console.Write ("You can say nothing."); #endif console.readline (); }}}//output: Hello Copy code from the above code we can see that the first line defines a symbolic debug, and then the second line cancels the definition of the symbol, which is equivalent to no definition, so the program does not execute Console.Write ("debug .") This statement. The third line defines the Issay symbol, so the program outputs "Hello", and if we annotate it or delete it, the program will output "You can say nothing". We can initially see that by defining pre-compiled symbols, you can control the compiler to selectively compile code. The code above also has # if and #endif.Symbols, these are conditional compilation directives. 2. Conditional compilation Directive conditional compilation directives There are 4, except for the # if, #else, #endif之外 that we see from the first example, and a #elif. We should have been familiar with these instructions, they and we usually write code when the conditional statement is the same, the conditional statements are used to control the flow of the program, and these conditional compilation directives are used for controlling the compiler to selectively compile the code. An # If statement can have 0 or more #elif statements, or there can be 0 or one #else statement, but must include a #endif statement that must have, or a syntax error will occur. 3. #region和 #endregion These two symbols usually we use a lot of, is to put some related code folded together, so that we write a long code in a file is very useful, we can put a set of related code with #region and # Endregion are organized together and can be appended to the #region text, and when this set of code is folded up, we can see the explanatory text behind #region. The C # Preprocessor directives on MSDN describe visual Studio 2013MSDN This section contains information about the following C # preprocessor directives. #if #else#elif#endif# Define#undef#warning#error#line#region#endregion#pragma#pragma Warning#pragma Checksum see individual topics for more information and examples. Although the compiler does not have a separate preprocessor, the processing instructions described in this section are like the one. They are used in conditional compilation for help. Unlike C and C + + directives, you cannot use these directives to create macros. The preprocessor directive must be the only instruction on the line. Good text to the top of my attention
C # Preprocessor Directives