C # preprocessor directives are never translated into executable code commands, but can affect all aspects of the compilation process, common preprocessor directives have # define, #undef, #if, #elif, #else和 #endif, and so on, the following describes C # use # Define an instance of conditional compilation.
In C #, conditional compilation directives are used to conditionally include or exclude portions of a source file. In Visual Studio, you see that the excluded code appears dimmed.
First, #define可以用来做什么
1, when the plan to release two versions of the code. That is, the basic version and have more versions of the Enterprise Edition, you can use the conditional compilation instructions;
2, example as a file for Silverlight, WPF, WinForm and so on, and also consider debug and release, and so on, most of the code is the same;
3. Specify whether the functions and attributes are compiled into the final product.
Second, #define用法
Syntax: #define Name
Note: Here is the name of debug, you can also take other names such as Dragon
1 #define Debug
Description
1, debug can be seen as a declaration of a variable, but this variable does not have a real value, if the debug result is true, otherwise false;
2, #define单独用没什么意义, generally with # if or conditional features combined use;
3, #define必须定义在所有using命名空间前面;
4,debug and debug are different, C # is case-sensitive.
Third, #define条件编译实例
Method One, use # if
1 #define Dragon 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Diagnostics; 7 8 Namespace Conditionalcompilation 9 {Ten class Program11 {$ static void Main (string[] args) #if Dragon15 Console.WriteLine ("Dragon is defined"), #else17 Console.WriteLine ("Dragon is not defined") ; #endif19 Console.readkey (); }21 }22}
The output results are as follows:
If the comment is dropped//#define DRAGON, the output is:
Mode two, the use of conditional characteristics
We can isolate some functions so that they work only after defining certain environment variables or setting a value, and the isolation strategy using the Conditional attribute is #endif不容易出错 than #if/.
1 #define DEBUG 2 #define TRACE 3 #if (Debug && Trace) 4 #define DEBUGANDTRACE 5 #endif 6 using System; 7 using System.Collections.Generic; 8 using System.Linq; 9 using system.text;10 using System.diagnostics;11-namespace ConditionalCompilation13 {+ class Program15 {16 static void Main (string[] args): {Print0 (); Print1 (); Print2 (); Print3 (); Console.readkey ();}24 [Conditional ("DEBUG")]26 static void Print0 () Console.WriteLine ("DEBUG is defined"),}30 [Conditional ("Deb UG ")]32 static void Print1 () {Console.WriteLine (" Debug is defined "); 35}36 37 Debug or trace is defined to execute this method 38//or the relationship of the [Conditional ("Debug"), Conditional ("Trace")]40 Static V OID Print2 () Console.WriteLine ("Debug or Trace is defined"); 43}44 45//Only after Debug and trace is defined does this method 46//And the relationship of the [Conditional ("Debugandtrace")]48 static void Print3 () Console.WriteLine ("Debug and Trace is defined"); 51}52}53}
The output results are as follows:
Description
1, the code does not define debug, but the output of debug, because the debug version, automatic definition of Debug. The check box in front of the Define DEBUG constant (U) under Project-right--Properties--Build tab--General bar is selected. Of course you can remove the selected state so that no debug is output.
2. If neither debug nor trace is defined, Debug or trace will not be output and debug and trace will be output only if both Debug and trace are defined.
3. You can add multiple attributes to Conditional, such as example code [Conditional ("Debug"), Conditional ("Trace")], but the relationship between multiple attributes is or, that is, "Debug" or any one of the "Trace" is defined, then the corresponding method is executed.
4, if you need to add multiple attributes, directly with conditional is not possible, you need to use # if/#endif间接来完成, as in the sample code of the combined Operation
1 #if (Debug && Trace) 2 #define DEBUGANDTRACE3 #endif
using Conditional the method of the property is limited by the following:
1. The conditional method must be a method in a class declaration or struct declaration. If you specify the conditional property on a method in an interface declaration, a compile-time error occurs;
2, the conditional method cannot be the implementation of the interface method. Otherwise, a compile-time error occurs;
3. The conditional method must have a void return type;
4. You cannot mark conditional methods with the override modifier. However, you can mark conditional methods with the virtual modifier. The overriding method of such a method is implied by a conditional method and cannot be explicitly marked with the conditional property.
Environment variables (or conditional compilation symbols) are set up in three ways:
1) Define with # define and #undef, defined in front of all using namespaces;
2) with compiler command-line options (for example,/define:debug), set in conditional compilation symbols (Y) under Project-right--Properties--Build Tab-General bar (if multiple, you can separate them with commas). Debug and Trace are set by default in the debug version;
3) Use the environment variables in the operating system shell (for example, set debug=1).
Reference link ①:http://www.studyofnet.com/news/1240.html
Reference link ②:http://blog.csdn.net/teng_s2000/article/details/5510420
Reference link ③:https://msdn.microsoft.com/zh-cn/library/system.diagnostics.conditionalattribute.aspx
c#-#define条件编译