c#-#define条件编译

Source: Internet
Author: User

This article is guided by:

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 #defineDragon2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Diagnostics;7 8 namespaceconditionalcompilation9 {Ten     class Program One     { A         Static voidMain (string[] args) -         { - #ifDragon theConsole.WriteLine ("Dragon is defined"); - #else -Console.WriteLine ("Dragon is not defined"); - #endif + Console.readkey (); -         } +     } A}

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 #defineDebug2 #defineTrace3 #if(Debug && Trace)4 #defineDebugandtrace5 #endif6 usingSystem;7 usingSystem.Collections.Generic;8 usingSystem.Linq;9 usingSystem.Text;Ten usingSystem.Diagnostics; One  A namespaceconditionalcompilation - { -     class Program the     { -         Static voidMain (string[] args) -         { - Print0 (); + Print1 (); - Print2 (); + Print3 (); A Console.readkey (); at         } -  -[Conditional ("DEBUG")] -         Static voidPrint0 () -         { -Console.WriteLine ("DEBUG is defined"); in         } -  to[Conditional ("Debug")] +         Static voidPrint1 () -         { theConsole.WriteLine ("Debug is defined"); *         } $ Panax Notoginseng         //This method is not executed until Debug or trace is defined -         //or the relationship the[Conditional ("Debug"), Conditional ("Trace")] +         Static voidPrint2 () A         { theConsole.WriteLine ("Debug or Trace is defined"); +         } -  $         //This method is not executed until debug and trace have been defined $         //and the relationship -[Conditional ("Debugandtrace")] -         Static voidPrint3 () the         { -Console.WriteLine ("Debug and Trace is defined");Wuyi         } the     } -}

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 is a relationship, that is,"Debug "or"Trace "any one is defined, then the corresponding method will be 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条件编译

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.