Multiplicative * / %
Additive + -
Shift << >>
Relational and type testing < > <= >> is as
Equality == !=
Logical AND &
Logical XOR ^
Logical OR |
Conditional AND &&
Conditional OR ||
Conditional ?:
Assignment = *= /= %= += -= <<= >>= &= ^= | =

When you are in doubt of the precedence of two operators, always use parentheses to force the compiler to evaluate the expression first. For example, the formula to convert a temperature from Fahrenheit to Celsius is:

Tc = (5/9)*(Tf-32);

When implemented in C#, the formula looks like this:

double fahrenheit = 100;

double celcius = 5.0 / 9.0 * fahrenheit - 32;

Console.WriteLine('{0:##.##} degrees C',celcius); //---23.56 degrees C---

But this produces a wrong answer because 5.0 / 9.0 and fahrenheit - 32 must be evaluated separately before their results are multiplied to get the final answer. What's happened is that, according to the precedence table, 5.0 / 9.0 * fahrenheit is evaluated first and then 32 is subtracted from the result. This gives the incorrect answer of 23.56 degrees C.

To correct this, you use parentheses to group all the expressions that need to be evaluated first, like this:

double fahrenheit = 100;

double celcius = (5.0 / 9.0) * (fahrenheit - 32);

Console.WriteLine('{0:##.##} degrees C',celcius); //---37.78 degrees C---

This code gives the correct answer of 37.78 degrees C.

Preprocessor Directives

So far the programs you have seen in this chapter are pretty straightforward; you compile the entire program and run it from beginning until end. However, there are times when you want to inject debugging statements into your program — generally using methods such as Console.WriteLine() or MessageBox.Show() — and then remove them when the program is ready for deployment. But one common mistake is that programmers often forget to remove all those statements after debugging. The end result is that production code often contains many redundant code statements.

A better way is to instruct the C# compile to conditionally omit some of the code during compilation. For example, you can delineate some parts of your code as debugging statements that should not be present in the production code. To do so, you can use preprocessor directives, which are special instructions to a special program (known as the processor) that will prepare your code before sending it to the compiler. C# supports the following preprocessor directives, most of which are discussed in the following sections:

#define #elif    #line   #pragma warning

#undef  #endif   #region #pragma checksum

#if     #warning #endregion

#else   #error   #pragma

#define and #undef

The #define preprocessor directive allows you to define a symbol so that you can use the #if preprocessor directive to evaluate and then make conditional compilation. To see how the #define preprocessor directive works, assume that you have a console application named TestDefine (saved in C:) created using Visual Studio 2008 (see Figure 3-12).

Figure 3-12

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату