Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 23 PREPROCESSOR DIRECTIVES

Conditional Compilation

Conditional compilation allows you to mark a section of source code to be either compiled or skipped, depending on whether a particular compilation symbol is defined.

There are four directives for specifying conditional compilation:

#if

#else

#elif

#endif

A condition is a simple expression that returns either true or false.

A condition can consist of a single compilation symbol or an expression of symbols and operators, as summarized in Table 23-2. Subexpressions can be grouped with parentheses.

The literals true and false can also be used in conditional expressions.

Table 23-2. Conditions Used in the #if and #elif Directives

Parameter Type

Meaning

Evaluation

Compilation symbol

Identifier, defined (or not) using

True: If the symbol has been defined

 

the #define directive

using a #define directive

 

 

False: Otherwise

Expression

Constructed using symbols and

True: If the expression evaluates to true

 

the operators !, ==, !=, &&, ||

False: Otherwise

 

 

 

The following are examples of conditional compilation conditions:

Expression

#if !DemoVersion

...

#endif Expression

#if (LeftHanded && OemVersion) || FullVersion

...

#endif

#if true

// The following code segment will always be compiled.

...

 

 

 

 

#endif

 

 

631

CHAPTER 23 PREPROCESSOR DIRECTIVES

The Conditional Compilation Constructs

The #if and #endif directives are the matching demarcations of a conditional compilation construct. Whenever there is an #if directive, there must also be a matching #endif.

Figure 23-1 illustrates the #if and #if...#else constructs.

If the condition in the #if construct evaluates to true, the code section following it is compiled. Otherwise, it is skipped.

In the #if...#else construct, if the condition evaluates to true, CodeSection1 is compiled. Otherwise, CodeSection2 is compiled.

Figure 23-1. The #if and #else constructs

For example, the following code illustrates a simple #if...#else construct. If the symbol RightHanded is defined, the code between the #if and the #else will be compiled. Otherwise, the code between the #else and the #endif will be compiled.

...

#if RightHanded

// Code implementing right-handed functionality

...

#else

// Code implementing left-handed functionality

...

#endif

632

CHAPTER 23 PREPROCESSOR DIRECTIVES

Figure 23-2 illustrates the #if...#elif and #if...#elif...#else constructs.

In the #if...#elif construct, if Cond1 evaluates to true, CodeSection1 is compiled, and compilation continues after the #endif.

Otherwise, if Cond2 evaluates to true, CodeSection2 is compiled, and compilation continues after the #endif.

This continues until either a condition evaluates to true or all the conditions have returned false. If that’s the case, none of the code sections in the construct are compiled, and compilation continues after the #endif.

The #if...#elif...#else construct works the same way, except that if no condition is true, then the code section after the #else is then compiled, and compilation continues after the #endif.

Figure 23-2. The #elif construct

633

CHAPTER 23 PREPROCESSOR DIRECTIVES

The following code demonstrates the #if...#elif...#else construct. The string containing the description of the version of the program is set to various values, depending on which compilation symbol is defined.

#define DemoVersionWithoutTimeLimit

 

...

const int intExpireLength = 30;

string strVersionDesc = null;

int

intExpireCount = 0;

#if

DemoVersionWithTimeLimit

intExpireCount = intExpireLength;

strVersionDesc = "This version of Supergame Plus will expire in 30 days";

#elif DemoVersionWithoutTimeLimit

strVersionDesc = "Demo Version of Supergame Plus";

#elif OEMVersion

strVersionDesc = "Supergame Plus, distributed under license";

#else

strVersionDesc = "The original Supergame Plus!!"; #endif

Console.WriteLine( strVersionDesc );

...

634

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]