C #-# define condition compilation,

Source: Internet
Author: User

C #-# define condition compilation,

Introduction:

C # pre-processor commands will never be converted into executable code commands, but will affect all aspects of the compilation process. Commonly used pre-processor Commands include # define, # undef, # if, # elif, # else and # endif, etc. The following describes the example of using # define in C # For Conditional compilation.

In C #, the Conditional compilation command is used to include or exclude certain parts of the source file according to the conditions. In Visual Studio, the excluded code is displayed in gray.

I. # What can define do?

1. When two versions of code are planned to be released. That is, the basic edition and the Enterprise Edition with more versions can use the Conditional compilation command;

2. For example, if the same file is used for silverlight, wpf, winform, and Debug and Release, most of the Code is the same;

3. Specify whether the functions and attributes are compiled into the final product.

Ii. # define usage

Syntax: # define name

Note: The name here is Debug. You can also use another name, such as Dragon.

1 #define Debug

Note:

1. Debug can be seen as a declared variable, but this variable has no real value,If yes # if Debug returns true; otherwise, false;

2. # It doesn't make sense to use define independently. It is generally used in combination with the # if or Conditional feature;

3. # define must be defined before all using namespaces;

4,DebugAndDEBUGC # Is case sensitive.

Iii. # define condition compiling example

Method 1: 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 {10     class Program11     {12         static void Main(string[] args)13         {14 #if Dragon15             Console.WriteLine("Dragon is defined");16 #else17             Console.WriteLine("Dragon is not defined");18 #endif19             Console.ReadKey();20         }21     }22 }

The output result is as follows:

If you comment out // # define Dragon, the output result is:

Method 2: Use the Conditional feature

We can isolate some functions so that they can only take effect after some environment variables are defined or a value is set, the isolation policy using the Conditional feature is more error-prone than # if/# endif.

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 12 namespace ConditionalCompilation13 {14 class Program15 {16 static void Main (string [] args) 17 {18 Print0 (); 19 Print1 (); 20 Print2 (); 21 Print3 (); 22 Console. readKey (); 23} 24 25 [Conditional ("DEBUG")] 26 static void Print0 () 27 {28 Console. writeLine ("DEBUG is defined"); 29} 30 31 [Conditional ("Debug")] 32 static void Print1 () 33 {34 Console. writeLine ("Debug is defined "); 35} 36 37 // the relationship between this Method 38 // or 39 after the Debug or Trace is defined [Conditional ("Debug"), Conditional ("Trace")] 40 static void Print2 () 41 {42 Console. writeLine ("Debug or Trace is defined "); 43} 44 45 // This method is executed only after Debug and Trace are defined. 46 // and the relationship between them is 47 [Conditional ("DebugAndTrace")] 48 static void Print3 () 49 {50 Console. writeLine ("Debug and Trace is defined"); 51} 52} 53}

The output result is as follows:

Note:

1. No DEBUG is defined in the Code, but the DEBUG is output because the DEBUG version automatically defines the DEBUG. The check box before defining DEBUG constant (U) under "project -- Right-click -- Property -- generate tab -- General bar" is selected. Of course, you can remove the selected status so that no DEBUG is output.

2. If neither Debug nor Trace is defined, Debug or Trace is not output. Only Debug and Trace are defined.

3. You can add multiple attributes to Conditional, such as the sample code [Conditional ("Debug"), Conditional ("Trace")]. However, the relationship between multiple attributes is or, that is, if either "Debug" or "Trace" is defined, the corresponding method will be executed.

4. if you need to add multiple and attributes, directly using Conditional cannot be implemented. You need to use # if/# endif to indirectly complete the process, such as the combined operation in the sample code.

1 #if (Debug && Trace)2 #define DebugAndTrace3 #endif

Use ConditionalAttribute methods are subject to the following restrictions:

1. The condition method must be a method in the class declaration or structure declaration. If the Conditional attribute is specified on the method in the interface declaration, a compile-time error occurs;

2. The condition method cannot be the implementation of the interface method. Otherwise, a compilation error will occur;

3. The condition method must have the void return type;

4. The condition method cannot be marked with the override modifier. However, you can use the virtual modifier to mark the condition method. The override method of this type of method is implicitly a Conditional method, and it cannot be explicitly marked with the Conditional attribute.

Environment variables (or Conditional compilation symbols) can be set in three ways:

1) Use # define and # undef to cancel the definition, which is defined before all using namespaces;

2) use the compiler command line option (for example,/define: DEBUG) to compile the condition symbol (Y) under "project -- Right-click -- Property -- generate tab -- General bar) ). In the DEBUG version, DEBUG and TRACE are set by default;

3) use environment variables in the operating system shell program (for example, set DEBUG = 1 ).

Reference ①:Http://www.studyofnet.com/news/1240.html

Reference ②:Http://blog.csdn.net/teng_s2000/article/details/5510420

Reference ③:Https://msdn.microsoft.com/zh-cn/library/system.diagnostics.conditionalattribute.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.