Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
26
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

Chapter 2 Working with Variables, Operators, and Expressions

43

The fourth statement subtracts the value of the rhs variable from the value of the lhs variable and assigns the result to outcome.

The fifth statement concatenates three strings indicating the calculation being performed (using the plus operator, +) and assigns the result to the expression.Text property. This causes the string to appear in the expression text box on the form.

The sixth statement displays the result of the calculation by assigning it to the Text property of the result text box. Remember that the Text property is a string and that

the result of the calculation is an int, so you must convert the string to an int before assigning it to the Text property. This is what the ToString method of the int type does.

The ToString Method

Every class in the .NET Framework has a ToString method. The purpose of ToString is to convert an object to its string representation. In the preceding example, the ToString method of the integer object, outcome, is used to convert the integer value of outcome

to the equivalent string value. This conversion is necessary because the value is displayed in the Text property of the result text box—the Text property can contain only

strings. When you create your own classes, you can define your own implementation of the ToString method to specify how your class should be represented as a string.

You learn more about creating your own classes in Chapter 7, “Creating and Managing Classes and Objects.”

Controlling Precedence

Precedence governs the order in which an expression’s operators are evaluated. Consider the following expression, which uses the + and * operators:

2 + 3 * 4

This expression is potentially ambiguous; do you perform the addition first or the multiplication? In other words, does 3 bind to the + operator on its left or to the * operator on its right? The order of the operations matters because it changes the result:

If you perform the addition first, followed by the multiplication, the result of the addition (2 + 3) forms the left operand of the * operator, and the result of the whole expression is 5 * 4, which is 20.

If you perform the multiplication first, followed by the addition, the result of the multiplication (3 * 4) forms the right operand of the + operator, and the result of the whole expression is 2 + 12, which is 14.

44

Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008

 

In C#, the multiplicative operators (*, /, and %) have precedence over the additive operators

 

(+ and –), so in expressions such as 2 + 3 * 4, the multiplication is performed first, followed

 

by the addition. The answer to 2 + 3 * 4 is therefore 14. As each new operator is discussed in

 

later chapters, its precedence will be explained.

 

You can use parentheses to override precedence and force operands to bind to operators in

 

a different way. For example, in the following expression, the parentheses force the 2 and the

 

3 to bind to the + operator (making 5), and the result of this addition forms the left operand

 

of the * operator to produce the value 20:

 

(2 + 3) * 4

 

Note The term parentheses or round brackets refers to ( ). The term braces or curly brackets

 

refers to { }. The term square brackets refers to [ ].

Using Associativity to Evaluate Expressions

Operator precedence is only half the story. What happens when an expression contains

different operators that have the same precedence? This is where associativity becomes important. Associativity is the direction (left or right) in which the operands of an operator

are evaluated. Consider the following expression that uses the / and * operators:

4 / 2 * 6

This expression is still potentially ambiguous. Do you perform the division first, or the multiplication? The precedence of both operators is the same (they are both multiplicative), but the order in which the expression is evaluated is important because you get one of two possible results:

If you perform the division first, the result of the division (4/2) forms the left operand of the * operator, and the result of the whole expression is (4/2) * 6, or 12.

If you perform the multiplication first, the result of the multiplication (2 * 6) forms the right operand of the / operator, and the result of the whole expression is 4/(2 * 6), or 4/12.

In this case, the associativity of the operators determines how the expression is evaluated. The * and / operators are both left-associative, which means that the operands are evaluated from left to right. In this case, 4/2 will be evaluated before multiplying by 6, giving the result 12. As each new operator is discussed in subsequent chapters, its associativity is also covered.

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