| [ directory ] |
|
8.8 Assignment OperatorsC# and Java share a common set of assignment operators, which are explained in the following sections. 8.8.1 The = OperatorThis binary operator stores the value of the operand on the RHS in the indexer, property, or variable defined on the LHS. For this assignment to be successful, either the two operands should be of the same data type or the LHS operand should have an implicit conversion to the RHS operand. This operator cannot be overloaded. 8.8.2 The += OperatorThe addition assignment operator is a binary operator that increments the operand on the LHS by an amount indicated by the operand on the RHS and then assigns the result to the LHS. This operator cannot be overloaded. x += y implies x = x + y 8.8.3 The -= OperatorThe subtraction assignment operator is a binary operator that decrements the operand on the LHS by an amount indicated by the operand on the RHS and then assigns the result to the LHS. This operator cannot be overloaded. x -= y implies x = x - y 8.8.4 The *= OperatorThe multiplication assignment operator is a binary operator that multiplies the operand on the LHS by an amount indicated by the operand on the RHS and then assigns the result to the LHS. This operator cannot be overloaded. x *= y implies x = x * y 8.8.5 The /= OperatorThe division assignment operator is a binary operator that divides the operand on the LHS by an amount indicated by the operand on the RHS and then assigns the result to the LHS. This operator cannot be overloaded. x /= y implies x = x / y 8.8.6 The %= OperatorThe modulus assignment operator is a binary operator that performs a modulus operation on the operand on the LHS by an amount indicated by the operand on the RHS and then assigns the result to the LHS. This operator cannot be overloaded. x %= y implies x = x % y 8.8.7 The &= OperatorThe logical AND assignment operator is a binary operator that performs a logical AND of the operand on the LHS with the RHS operand and then assigns the result to the LHS operand. This operator cannot be overloaded. x &= y implies x = x & y 8.8.8 The |= OperatorThe logical OR assignment operator is a binary operator that performs a logical OR of the operand on the LHS with the RHS operand and then assigns the result to the LHS operand. This operator cannot be overloaded. x |= y implies x = x | y 8.8.9 The ^= OperatorThe logical exclusive-OR assignment operator is a binary operator that performs a logical exclusive-OR of the operand on the LHS with the RHS operand and then assigns the result to the LHS operand. This operator cannot be overloaded. x ^= y implies x = x ^ y 8.8.10 The >>= OperatorThe shift assignment operator is a binary operator that shifts the LHS operand right by an amount indicated by the RHS operand and then assigns the result to the LHS operand. This operator cannot be overloaded. x >>= y implies x = x >> y 8.8.11 The <<= OperatorThe shift assignment operator is a binary operator that shifts the LHS operand left by an amount indicated by the RHS operand and then assigns the result to the LHS operand. This operator cannot be overloaded. x <<= y implies x = x << y Listing 8.9 shows the assignment operators. Listing 8.9 Assignment Operators (C#)
using System;
class Test {
public static void Main(string[] args) {
int a = 8;
Console.WriteLine(a);
Console.WriteLine(a += 2);
Console.WriteLine(a -= 2);
Console.WriteLine(a *= 2);
Console.WriteLine(a /= 2);
Console.WriteLine(a %= 2);
Console.WriteLine(a &= 2);
Console.WriteLine(a |= 2);
Console.WriteLine(a ^= 2);
Console.WriteLine(a >>= 2);
Console.WriteLine(a <<= 2);
}
}
The output of Listing 8.9 is as follows: 8 10 8 16 8 0 0 2 0 0 0 |
| [ directory ] |
|