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

C H A P T E R 23

■ ■ ■

Preprocessor Directives

What Are Preprocessor Directives?

General Rules

The #define and #undef Directives

Conditional Compilation

The Conditional Compilation Constructs

Diagnostic Directives

Line Number Directives

Region Directives

The #pragma warning Directive

627

CHAPTER 23 PREPROCESSOR DIRECTIVES

What Are Preprocessor Directives?

The source code specifies the definition of a program. The preprocessor directives instruct the compiler how to treat the source code. For example, under certain conditions, you might want the compiler to ignore portions of the code, and under other conditions, you might want that code compiled. The preprocessor directives give you those options and several others.

In C and C++ there is an actual preprocessor phase, in which the preprocessor goes through the source code and prepares an output stream of text to be processed by the subsequent compilation phase. In C# there is no actual preprocessor. The “preprocessor” directives are handled by the compiler. The term, however, remains.

General Rules

Some of the most important syntactic rules for preprocessor directives are the following:

Preprocessor directives must be on lines separate from C# code.

Unlike C# statements, preprocessor directives are not terminated with a semicolon.

Every line containing a preprocessor directive must start with the # character.

There can be space before the # character.

There can be space between the # character and the directive.

End-of-line comments are allowed.

Delimited comments are not allowed in a preprocessor directive line.

628

CHAPTER 23 PREPROCESSOR DIRECTIVES

Here are some examples illustrating the rules:

No semicolon

 

 

 

 

 

 

#define PremiumVersion

 

 

// OK

Space before

 

 

 

 

 

 

 

 

 

 

 

 

#define BudgetVersion

 

 

// OK

# define MediumVersion

 

 

// OK

 

 

 

 

 

 

Space between

 

Delimited comments are not allowed.

 

 

 

 

 

#define PremiumVersion

/* all bells & whistles */

 

 

 

 

End-of-line comments are fine.

 

 

 

 

 

 

 

#define BudgetVersion

 

// Stripped-down version

The preprocessor directives are listed in Table 23-1.

Table 23-1. Preprocessor Directives

 

 

Directive

Summary of Meaning

 

 

#define identifier

Defines a compilation symbol

#undef identifier

Undefines a compilation symbol

#if expression

If the expression is true, compiles the following section

#elif expression

If the expression is true, compiles the following section

#else

If the previous #if or #elif expression is false, compiles the following section

#endif

Marks the end of an #if construct

#region name

Marks the beginning of a region of code; has no compilation effect

#endregion name

Marks the end of a region of code; has no compilation effect

#warning message

Displays a compile-time warning message

#error message

Displays a compile-time error message

#line indicator

Changes the line numbers displayed in compiler messages

#pragma text

Specifies information about the program context

 

 

 

 

 

 

 

629

CHAPTER 23 PREPROCESSOR DIRECTIVES

The #define and #undef Directives

A compilation symbol is an identifier that has only two possible states. It is either defined or undefined. A compilation symbol has the following characteristics:

It can be any identifier except true or false. This includes C# keywords and identifiers declared in your C# code—both of which are fine.

It has no value. Unlike in C and C++, it does not represent a string.

As shown in Table 23-1:

The #define directive declares a compilation symbol.

The #undef directive undefines a compilation symbol.

#define PremiumVersion #define EconomyVersion

...

#undef PremiumVersion

The #define and #undef directives can be used only at the top of a source file, before any C# code is listed. After the C# code has started, the #define and #undef directives can no longer be used.

using System;

// First line of C# code

#define PremiumVersion

// Error

namespace Eagle

 

{

 

#define PremiumVersion

// Error

...

 

The scope of a compilation symbol is limited to a single source file. Redefining a symbol that is already defined is perfectly fine—as long as it’s before any C# code, of course.

#define AValue #define BValue

#define AValue

// Redefinition is fine.

630

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