Changes

Jump to: navigation, search

C Sharp Operators and Expressions

1,253 bytes added, 19:57, 14 January 2008
Compound Assignment Operators
<td>x ^= y<td>Assign to x the result of logical Exclusive OR on x and y</td>
</table>
 
== Increment and Decrement Operators ==
 
Another useful shortcut can be achieved using the C# increment and decrement operators. As with the compound assignment operators described in the previous section, consider the following C# code fragment:
 
<pre>
x = x + 1; // Increase value of variable x by 1
 
x = x - 1; // Decrease value of variable y by 1
</pre>
 
These expressions increment and decrement the value of x by 1. Instead of using this appraoch it is quicker to use the ++ and -- operators. The following examples perform exactly the same tasks as the examples above:
 
<pre>
x++; Increment x by 1
 
x--; Decrement x by 1
</pre>
 
These operators can be placed either before or after the variable name. If the operator is place before the variable name the increment or decrement is performed before any other operations are performed on the variable. For example, in the following example, x is incremented before it is assigned to y, leaving y with a value of 10:
 
<pre>
int x = 9;
int y;
 
y = ++x;
</pre>
 
In the following example, the value of x (9) is assigned to variable y ''before'' the decrement is performed. After the expression is evaluated the value of y will be 9 and the value of x will 8.
 
<pre>
int x = 9;
int y;
 
y = x--;
</pre>

Navigation menu