C # Conditional compilation

Source: Internet
Author: User
The Conditional compilation command is used to include or exclude certain parts of the source file based on conditions.

According to the syntax, the Conditional compilation command must be written in the form of a set. The composition of the set is:#ifCommand, one or more#elifCommand (or none), one or more#elseCommand (or none) and one#endifCommand. Commands are the source code condition section. Each code is directly controlled by the command at the front of it. The condition section itself can contain nested condition compilation instructions, provided that these commands constitute a complete instruction set.

1 # define debug // debugging on
2 # UNDEF trace // tracing off
3 class purchasetransaction
4 {
5 void commit (){
6 # If debug
7 checkconsistency ();
8 # If trace
9 writetolog (this. tostring ());
10 # endif
11 # endif
12 commithelper ();
13}
14}

# Define or # UNDEF must be defined before all using namespaces. # define type is Boolean true, # UNDEF is Boolean false.

C # a better choice is proposed for this purpose: the conditional feature. Using the conditional feature, we can isolate some functions so that they can only take effect after defining certain environment variables or setting a certain value. The most common feature of conditional is to adapt the Code to a debugging statement .. . NET Framework has provided support for related functions. The following code demonstrates the working principle of the conditional feature and its applicability.

1 [conditional ("debug")]
2 Private void checkstate ()
3 {
4 // The Code remains unchanged. Www.elivn.com
5}
6
7

Namespace of conditional: using system. diagnostics;
The conditional method must be of the void return type.

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.