Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

CSharp Bible (2002) [eng]

.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
4.29 Mб
Скачать

If you are multiplying a value to a variable and placing the result in the same variable, you can write a shortcut statement to perform the multiplication. Writing an asterisk followed by an equals sign multiplies a value to a variable, and updates the variable's value with the result:

MyInteger *= 3;

This statement is shorthand for the following:

MyInteger = MyInteger * 3;

Using the division operator

The value of an expression using the division operator is the product of the values of the two operators. The forward slash character is used as the division operator, as shown in Listing 4- 12.

Listing 4-12: Division Operator (Example 1)

class MyClass

{

public static void Main()

{

int MyInteger;

MyInteger = 6 / 3; // MyInteger will be 2

}

}

If the division operation results in a remainder, only the quotient itself is the result of the operation (see Listing 4-13).

Listing 4-13: Division Operator (Example 2)

class MyClass

{

public static void Main()

{

int MyInteger;

MyInteger = 7 / 3;

}

}

When this code is executed, the MyInteger variable has a value of 2, because dividing 7 by 3 results in a quotient of 2 and a remainder of 1.

If you are dividing a value into a variable and placing the result in the same variable, you can write a shortcut statement to perform the division. Writing a forward slash character followed by an equals sign divides a value into a variable, and updates the variable's value with the result:

MyInteger /= 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger / 3;

Using the remainder operator

The value of an expression using the remainder operator is the remainder of a division operation. The percent character is used as the division operator (see Listing 4-14).

Listing 4-14: Remainder Operator

class MyClass

{

public static void Main()

{

int MyInteger;

MyInteger = 7 % 3;

}

}

When this code is executed, the MyInteger variable has value of 1, because dividing 7 by 3 results in a quotient of 2 and a remainder of 1.

If you are calculating a remainder using a variable and placing the result in the same variable, you can write a shortcut statement to perform the remainder operation. Writing a percent sign followed by an equals sign calculates the remainder from a variable and updates the variable's value with the result:

MyInteger %= 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger % 3;

Using the addition operator

The value of an expression using the addition operator is the sum of the values of the two operators. The plus character is used as the multiplication operator (see Listing 4-15).

Listing 4-15: Addition Operator

class MyClass

{

public static void Main()

{

int MyInteger;

MyInteger = 3 + 6; // MyInteger will be 9

}

}

If you are adding a value to a variable and placing the result in the same variable, you can write a shortcut statement to perform the addition. Writing a plus sign followed by an equals sign adds a value to a variable and updates the variable's value with the result:

MyInteger += 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger + 3;

The addition operator has special meaning when the two operands are strings. Adding two strings together concatenates the first string with the second string:

string CombinedString = "Hello from " + "C#";

The value of CombinedString is Hello from C# when this code is executed.

Using the subtraction operator

The value of an expression using the subtraction operator is the difference of the values of the two operators. The hyphen character is used as the subtraction operator (see Listing 4-16).

Listing 4-16: Subtraction Operator

class MyClass

{

public static void Main()

{

int MyInteger;

MyInteger = 7 - 3; // MyInteger will be 4

}

}

If you are subtracting a value from a variable and placing the result in the same variable, you can write a shortcut statement to perform the subtraction. Writing a minus sign followed by an equals sign subtracts a value from a variable and updates the variable's value with the result:

MyInteger -= 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger – 3;

Understanding Shift Operators

Shift operators enable you to move bits around in a value in your C# code. Expressions that use shift operators are binary expressions because two operands are required to perform a shift operation.

Moving bits with the shift-left operator

The value of an expression using the shift-left operator moves bits left by a specific amount. Two less-than characters (<<) are used as the shift-left operator (see Listing 4-17).

Listing 4-17: Shift-Left Operator

class MyClass

{

public static void Main()

{

int MyInteger;

MyInteger = 6 << 3;

}

}

When this code executes, the MyInteger variable has a value of 48, because the original value, 6, is viewed as a binary number with a binary value of 00000110. Each bit in the original value is shifted three places, which is the value shown after the shift left operator, and zeros are placed in the low bits. Shifting each bit three places gives a binary value of 00110000, or 48 decimal.

Expressions of type int, uint, long, and ulong can have left shifts applied to their values. Other expressions that can be converted to one of those types can be left shifted as well. Expressions of type int and uint can be shifted up to 32 bits at a time. Expressions of type long and ulong can be shifted up to 64 bits at a time.

If you are calculating a left-shift operation on a value and a variable and placing the result in the same variable, you can write a shortcut statement to perform the left-shift operation. Writing two less-than signs followed by an equals sign calculates the left-shift operation on a variable and a value and updates the variable's value with the result:

MyInteger <<= 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger << 3;

Moving bits with the shift-right operator

The value of an expression using the shift-right operator moves bits right by a specific amount. Two greater-than characters (>>) are used as the shift-right operator (see Listing 4- 18).

Listing 4-18: Shift-Right Operator

class MyClass

{

public static void Main()

{

int MyInteger;

MyInteger = 48 >> 3;

}

}

When this code executes, the MyInteger variable has a value of 6 because the original value, 48, is viewed as a binary number with a binary value of 00110000. Each bit in the original value is shifted three places, which is the value shown after the shift right operator, and zeros are placed in the high bits. Shifting each bit three places gives a binary value of 00000110, or 6 decimal.

Expressions of type int, uint, long, and ulong can have right shifts applied to their values. Other expressions that can be converted to one of those types can be right shifted as well. Expressions of type int and uint can be shifted up to 32 bits at a time. Expressions of type long and ulong can be shifted up to 64 bits at a time.

If you are calculating a right-shift operation on a value and a variable and placing the result in the same variable, you can write a shortcut statement to perform the right-shift operation. Writing two greater-than signs followed by an equals sign calculates the right shift operation on a variable and a value and updates the variable's value with the result:

MyInteger >>= 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger >> 3;

Comparing expressions with relational operators

Relational operators enable you to compare two expressions and obtain a Boolean value that specifies the relation between the two expressions. Expressions that use relational operators are binary expressions because two operands are required to perform a relational operation.

Testing for equality with the equality operator

The equality operator is used to test the values of two expressions for equality. If the expressions have the same value, the equality operator evaluates to True; if they are unequal, the equality operator evaluates to False. Two equals signs are used as the equality operator:

MyInteger == 123;

If the value of the MyInteger variable is 123, the equality operator evaluates to True. If it has any other value, the equality operator evaluates to False.

The equality operator has special meaning when the two operands are strings. Comparing two strings compares the string's contents. Two strings are considered equal if they have identical lengths and identical characters in each position of the string.

Testing for inequality with the inequality operator

The inequality operator is used to test the values of two expressions for inequality. If the expressions have different values, the inequality operator evaluates to True. If they are equal, the inequality operator evaluates to False. An exclamation point followed by an equals sign is used as the inequality operator:

MyInteger != 123;

If the value of the MyInteger variable is 123, the inequality operator evaluates to False. If it has any other value, the inequality operator evaluates to True.

The inequality operator has special meaning when the two operands are strings. Comparing two strings compares the string's contents. Two strings are considered unequal if they have different lengths or if they have different characters in at least one position of the string.

Testing values with the less-than operator

The less-than operator is used to test the values of two expressions to see if one value is less than the other value. If the first expression has a value less than the value of the second expression, the less-than operator evaluates to True. If the first expression has a value greater than or equal to the value of the second expression, the less-than operator evaluates to False. A less-than sign (<) is used as the less-than operator:

MyInteger < 123;

If the value of the MyInteger variable is less than 123, the less-than operator evaluates to True. If it has a value greater than or equal to 123, the less-than operator evaluates to False.

Testing values with the greater-than operator

The greater-than operator is used to test the values of two expressions to see whether one value is greater than the other value. If the first expression has a value greater than the second expression's value, the greater-than operator evaluates to True. If the first expression has a value less than or equal to the second expression's value, the greater-than operator evaluates to False. A greater-than sign (>) is used as the greater-than operator:

MyInteger > 123;

If the value of the MyInteger variable is greater than 123, the greater-than operator evaluates to True. If it has a value less than or equal to 123, the greater-than operator evaluates to False.

Testing values with the less-than-or-equal-to operator

The less-than-or-equal-to operator is used to test the values of two expressions to see whether one value is less than or equal to the other value. If the first expression has a value less than or

equal to the value of the second expression, the less-than-or-equal-to operator evaluates to True. If the first expression has a value greater than the value of the second expression, the less-than-or-equal-to operator evaluates to False. A less-than sign followed by an equals sign is used as the less-than-or-equal-to operator:

MyInteger <= 123;

If the value of the MyInteger variable is less than or equal to 123, the less-than-or-equal-to operator evaluates to True. If it has a value greater than 123, the less-than-or-equal-to operator evaluates to False.

Testing values with the greater-than-or-equal-to operator

The greater-than-or-equal-to operator is used to test the values of two expressions to see if one value is greater than or equal to the other value. If the first expression has a value greater than or equal to the value of the second expression, the greater-than-or-equal-to operator evaluates to True. If the first expression has a value less than the value of the second expression, the greater-than-or-equal-to operator evaluates to False. A greater-than sign followed by an equals sign is used as the greater-than-or-equal-to operator:

MyInteger >= 123;

If the value of the MyInteger variable is greater than or equal to 123, the greater-than-or- equal-to operator evaluates to True. If it has a value less than 123, the greater-than-or-equal-to operator evaluates to False.

Understanding Integer Logical Operators

Integer logical operators enable you to perform Boolean arithmetic on two numeric values. Expressions that use integer logical operators are binary expressions because two operands are required to perform a logical operation.

Computing Boolean values with the AND operator

The AND operator is used to compute the Boolean AND value of two expressions. The ampersand sign (&) is used as the AND operator:

MyInteger = 6 & 3;

The value of MyInteger is 2. Recall that a bit in an AND operation is 1 only if the two operand bits in the same position are 1. The value of 6 in binary is 110, and the value of 3 in binary is 011. Performing a Boolean AND of 110 and 011 results in a Boolean value of 010, or 2 in decimal.

If you are calculating an AND operation on a value and a variable and placing the result in the same variable, you can write a shortcut statement to perform the AND operation. Writing an ampersand character followed by an equals sign calculates the AND operation on a variable and a value, and updates the variable's value with the result:

MyInteger &= 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger & 3;

Computing Boolean values with the exclusive OR operator

The exclusive OR operator is used to compute the Boolean exclusive OR value of two expressions. The caret sign (^) is used as the exclusive OR operator:

MyInteger = 6 ^ 3;

The value of MyInteger is 5. Recall that a bit in an exclusive OR operation is 1 only if one of the two operand bits in the same position is 1. The value of 6 in binary is 110, and the value of 3 in binary is 011. Performing a Boolean exclusive OR of 110 and 011 results in a Boolean value of 101, or 5 in decimal.

If you are calculating an exclusive OR operation on a value and a variable and placing the result in the same variable, you can write a shortcut statement to perform the exclusive OR operation. Writing a caret sign followed by an equals sign calculates the exclusive OR operation on a variable and a value, and updates the variable's value with the result:

MyInteger ^= 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger ^ 3;

Computing Boolean values with the OR operator

The OR operator is used to compute the Boolean OR value of two expressions. The pipe character (|) is used as the OR operator:

MyInteger = 6 | 3;

The value of MyInteger is 7. Recall that a bit in an OR operation is 1 only if one or both of the two operand bits in the same position are 1. The value of 6 in binary is 110, and the value of 3 in binary is 011. Performing a Boolean OR of 110 and 011 results in a Boolean value of 111, or 7 in decimal.

If you are calculating an OR operation on a value and a variable and placing the result in the same variable, you can write a shortcut statement to perform the OR operation. Writing an ampersand character followed by an equals sign calculates the OR operation on a variable and a value, and updates the variable's value with the result:

MyInteger |= 3;

The preceding statement is shorthand for the following:

MyInteger = MyInteger | 3;

Understanding Conditional Logic Operators

Conditional logic operators are the conditional counterparts of the integer logical operators. Expressions that use conditional logical operators are binary expressions because two operands are required to perform a conditional logic operation.

Comparing Boolean values with the conditional AND operator

The conditional AND operator is used to compare two Boolean expressions. The result of the operation is True if both of the operands evaluate to True, and False if one or both of the operands evaluate to False. Two ampersand signs are used as the conditional AND operator:

MyBoolean = true && false;

The value of MyBoolean is False because one of the operands evaluates to False.

Comparing Boolean values with the conditional OR operator

The conditional OR operator is used to compare two Boolean expressions. The result of the operation is True if one or both of the operands evaluate to True, and False if both of the operands evaluate to False. Two pipe characters are used as the conditional OR operator:

MyBoolean = true || false;

The value of MyBoolean is True because one of the operands evaluates to True.

Comparing Boolean values with the conditional logic operator

The conditional logic operator evaluates a Boolean expression. The result of the expression has one value if the input expression evaluates to True and another if the input expression evaluates to False. Expressions that use conditional operators are tertiary expressions because three operands are required to perform a conditional logic operation. The conditional operator is the only tertiary expression supported in the C# language.

Writing a conditional operator involves writing the input expression, followed by a question mark. The True value comes next, followed by a colon, followed by the False value:

MyInteger = (MyVariable == 123) ? 3: 5;

You can read this statement as "Compare the value of MyVariable with 123. If that expression evaluates to True, set the value of MyInteger to 3. If that expression evaluates to False, set the value of MyInteger to 5."

Understanding the Order of Operations

C# enables you to place multiple operators in a single statement:

MyVariable = 3 * 2 + 1;

What is the value of MyVariable here? If C# applies the multiplication first, it reads the statement as "multiply 3 and 2 and then add 1," which results in a value of 7. If C# applies the addition first, it reads the statement as "add 2 and 1, and then multiply by 3," which results in a value of 9.

C# combines operators into groups and applies an order of precedence to each group. This order of precedence specifies which operators are evaluated before the others. The C# order of precedence list is as follows, listed in order from highest precedence to lowest precedence:

Primary expressions

Unary operators + - ! ~ ++ --

Multiplicative operators * / %

Additive operators + -

Shift operators << >>

Relational operators < > <= >=

Equality operators == !=

Logical AND

Logical exclusive OR

Logical OR

Conditional AND

Conditional OR

Conditional tertiary

Assignment operators

Take another look at the following statement:

MyVariable = 3 * 2 + 1

C# gives MyVariable a value of 7 because the multiplication operator has a higher precedence than the addition operator. This means that the multiplication operator is evaluated first, and the addition operator second.

You can override the order of precedence with parentheses. Expressions in parentheses are evaluated before the operator precedence rules are applied:

MyVariable = 3 * (2 + 1)

In this case, C# gives MyVariable a value of 9, because the addition expression is enclosed in parentheses, forcing it to be evaluated before the multiplication operation is evaluated.

Summary

C# defines many operators to help you evaluate your expressions and calculate new values from those operations. The language enables you to write expressions that perform mathematical functions and Boolean operations, and compare two expressions and obtain a Boolean result from the comparison.

In this chapter, you were introduced to the C# operators and you learned how to use those operators in expressions with literals and variables. You also reviewed operator expressions and the precedence of using these operators in expressions. When you examine classes in

Соседние файлы в предмете Программирование на C++