Operators (C# vs. Java)
C# offers all applicable operators supported by Java, as listed in the following table. At the end of the table, you will see some new operators available in C# but not Java:
Category | Symbol |
---|---|
Unary |
++ -- + - ! ~ () |
Multiplicative |
* / % |
Additive |
+ - |
Shift |
<< >> |
Relational |
< > <= >= instanceof |
Equality |
== != |
Logical AND |
& |
Logical XOR |
^ |
Logical OR |
| |
Conditional AND |
&& |
Conditional OR |
|| |
Conditional |
? : |
Assignment |
= *= /= %= += -= <<= >>= &= ^= |= |
Type of Operand |
typeof |
Size of Operand |
sizeof |
Enforce Overflow Checking |
checked |
Suppress Overflow Checking |
unchecked |
The only Java operator not available in C# is the shift operator (>>>). This operator is present in Java because of the lack of unsigned variables in that language, for cases when right-shifting is required to insert a 1 in the most significant bits.
C# supports unsigned variables, and therefore, C# only needs the standard >> operator. This operator produces different results depending on whether the operand is signed or unsigned. Right-shifting an unsigned number inserts 0 in the most significant bit while right-shifting a signed number copies the previous most significant bit.
Checked and Unchecked Operators
Arithmetic operations will result in overflow if the result is too large for the number of bits allocated to the data type in use. Such overflow can be checked or ignored for a given integral arithmetic operation using the checked and unchecked keywords. If the expression is a constant expression using checked, an error is generated at compile time.
The following is a simple example to illustrate these operators:
class TestCheckedAndUnchecked
{
static void Main()
{
short a = 10000;
short b = 10000;
short c = (short)(a * b); // unchecked by default
short d = unchecked((short)(10000 * 10000)); // unchecked
short e = checked((short)(a * b)); // checked - run-time error
System.Console.WriteLine(10000 * 10000); // 100000000
System.Console.WriteLine(c); // -7936
System.Console.WriteLine(d); // -7936
System.Console.WriteLine(e); // no result
}
}
In this code, the unchecked operator circumvents the compile-time error that would otherwise be caused by the following statement:
short d = unchecked((short)(10000 * 10000)); // unchecked
The next expression is unchecked by default, so the value silently overflows:
short c = (short)(a * b); // unchecked by default
We can force the expression to be checked for overflow at run time with the checked operator:
short e = checked((short)(a * b)); // checked - run-time error
Assigning the first two values to d
and c
silently overflows with a value of -7936 when the program is run, but when attempting to multiply the value for e using checked()
, the program will throw a OverflowException .
Note
You can also control whether to check for arithmetic overflow in a block of code by using the command-line compiler switch (/checked) or directly in Visual Studio on a per-project basis.