The Preprocessor


C# defines several preprocessor directives, which affect the way that your program’s source file is interpreted by the compiler. These directives act on the text of the source file in which they occur, prior to the translation of the program into object code. The preprocessor directives are largely a holdover from C++. In fact, the C# preprocessor is very similar to the one defined by C++. The term preprocessor directive comes from the fact that these instructions were traditionally handled by a separate compilation phase called the preprocessor. Today’s modern compiler technology no longer requires a separate preprocessing stage to handle the directives, but the name has stuck.

C# defines the following preprocessor directives:

#define

#elif

#else

#endif

#endregion

#error

#if

#line

#pragma

#region

#undef

#warning

All preprocessor directives begin with a # sign. In addition, each preprocessor directive must be on its own line. The #pragma directive was added by C# 2.0.

Frankly, given C#’s modern, object-oriented architecture, there is not as much need for the preprocessor directives as there is in older languages. Nevertheless, they can be of value from time to time, especially for conditional compilation. Each directive is examined in turn.

#define

The #define directive defines a character sequence called a symbol. The existence or nonexistence of a symbol can be determined by #if or #elif and is used to control compilation. Here is the general form for #define:

 #define symbol 

Notice that there is no semicolon in this statement. There may be any number of spaces between the #define and the symbol, but once the symbol begins, it is terminated only by a newline. For example, to define the symbol EXPERIMENTAL, use this directive:

 #define EXPERIMENTAL



C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net