Compound Assignment
Beside simple assignment, the language also supports compound assignment. This form of assignment allows the use of a binary operator to be combined with assignment at one time and has the following general form (where op stands for a binary operator):
x op= y
In the simplest case, it’s the equivalent to the more verbose form:
x = x op y
The reason I’m saying this is the simplest case is because rules exist that permit a conversion to take place before assigning the result to x. Here are a few examples to illustrate the use of compound assignments of various sorts:
int a = 42;a += 6; // a = 48a /= 8; // a = 6char c = 'z';c -= (char)2; // c = 'x'
Notice the explicit conversion of the integer literal 2 to a char on the last ...
Get C# 5.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.