15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The <strong>C#</strong> Preprocessor Directives ❘ 57<br />

// do something else<br />

#else<br />

// code for the leaner version<br />

#endif<br />

Note that, unlike the situation in C++, using #if is not the only way to compile code<br />

conditionally. <strong>C#</strong> provides an alternative mechanism through the Conditional attribute,<br />

which is explored in Chapter 14, “ Refl ection. ”<br />

#if <strong>and</strong> #elif support a limited range of logical operators too, using the operators ! , == , != , <strong>and</strong> || . A<br />

symbol is considered to be true if it exists <strong>and</strong> false if it doesn ’ t. For example:<br />

#if W2K & & (ENTERPRISE==false) // if W2K is defined but ENTERPRISE isn't<br />

#warning <strong>and</strong> #error<br />

Two other very useful preprocessor directives are #warning <strong>and</strong> #error . These will respectively cause<br />

a warning or an error to be raised when the compiler encounters them. If the compiler sees a #warning<br />

directive, it will display whatever text appears after the #warning to the user, after which compilation<br />

continues. If it encounters a #error directive, it will display the subsequent text to the user as if it were<br />

a compilation error message <strong>and</strong> then immediately ab<strong>and</strong>on the compilation, so no IL code will be<br />

generated.<br />

You can use these directives as checks that you haven ’ t done anything silly with your #define<br />

statements; you can also use the #warning statements to remind yourself to do something:<br />

#if DEBUG & & RELEASE<br />

#error "You've defined DEBUG <strong>and</strong> RELEASE simultaneously!"<br />

#endif<br />

#warning "Don't forget to remove this line before the boss tests the code!"<br />

Console.WriteLine("*I hate this job.*");<br />

#region <strong>and</strong> #endregion<br />

The #region <strong>and</strong> #endregion directives are used to indicate that a certain block of code is to be treated as<br />

a single block with a given name, like this:<br />

#region Member Field Declarations<br />

int x;<br />

double d;<br />

Currency balance;<br />

#endregion<br />

This doesn ’ t look that useful by itself; it doesn ’ t affect the compilation process in any way. However, the real<br />

advantage is that these directives are recognized by some editors, including the Visual Studio .<strong>NET</strong> editor.<br />

These editors can use these directives to lay out your code better on the screen. You will see how this works<br />

in Chapter 16.<br />

#line<br />

The #line directive can be used to alter the fi lename <strong>and</strong> line number information that is output by the<br />

compiler in warnings <strong>and</strong> error messages. You probably won ’ t want to use this directive that often. It ’ s most<br />

useful when you are coding in conjunction with some other package that alters the code you are typing<br />

in before sending it to the compiler. In this situation, line numbers, or perhaps the fi lenames reported by<br />

the compiler, won ’ t match up to the line numbers in the fi les or the fi lenames you are editing. The #line<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!