O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required