"Conditional Compilation"

Source: Internet
Author: User

Conditional compilation directives are used to include or exclude portions of a source file by criteria.

According to the syntax, conditional compilation directives must be written in the form of a set consisting of one #if instruction, one or more #elif instructions (or none), one or more instructions #else (or none), and one #endif instruction. Between directives is the conditional section of the source code. Each section of code is directly in the control of the command that precedes it. The conditional section itself can contain nested conditional compilation directives, provided that these directives form the complete set of instructions.

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 ());
Ten #endif
#endif
Commithelper ();
13}
14}

#define或 #undef must be defined in front of all using namespaces. #define类型为布尔值的真, #undef为布尔值的假.

C # has made a better choice for this: the conditional feature. Using the conditional feature, we can isolate some functions so that they only work if certain environment variables are defined or a value is set. The most common place to conditional features is to adapt the code to debug statements. NET Framework has provided the relevant functional support for this purpose. The following code shows how the conditional feature works and where applicable.

1 [Conditional ("DEBUG")]
2 private void CheckState ()
3 {
4//The code remains unchanged.
5}
6
7

Conditional namespace: Using System.Diagnostics;
The function of the conditional method must be a void return type.

This allows multiple versions of the program to be generated during compilation.

"Conditional Compilation"

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.