站内搜索: 请输入搜索关键词
当前页面: 图书首页 > NET For Java Developers Migrating To C#

NET For Java Developers Migrating To C#

[ directory ] Previous Section Next Section

8.8 Assignment Operators

C# and Java share a common set of assignment operators, which are explained in the following sections.

8.8.1 The = Operator

This 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 += Operator

The 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 -= Operator

The 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 *= Operator

The 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 /= Operator

The 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 %= Operator

The 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 &= Operator

The 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 |= Operator

The 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 ^= Operator

The 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 >>= Operator

The 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 <<= Operator

The 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 ] Previous Section Next Section