C# provides a large set of operators, which are symbols that specify which operations to perform in an expression. C# predefines the usual arithmetic and logical operators, as well as a variety of others as shown in the following table. In addition, many operators can be overloaded by the user, thus changing their meaning when applied to a user-defined type.
Operator category | Operators |
---|---|
Arithmetic | + - * / % |
Logical (boolean and bitwise) | & | ^ ! ~ && || true false |
String concatenation | + |
Increment, decrement | ++ -- |
Shift | << >> |
Relational | == != < > <= >= |
Assignment | = += -= *= /= %= &= |= ^= <<= >>= |
Member access | . |
Indexing | [] |
Cast | () |
Conditional | ?: |
Delegate concatenation and removal | + - |
Object creation | new |
Type information | is sizeof typeof |
Overflow exception control | checked unchecked |
Indirection and Address | * -> [] & |
The arithmetic operators (+, -, *, /) can produce results that are outside the range of possible values for the numeric type involved. You should refer to the C# Language Reference section on a particular operator for details, but in general:
When integer overflow occurs, what happens depends on the execution context, which can be checked or unchecked. In a checked context, an OverflowException is thrown. In an unchecked context, the most significant bits of the result are discarded and execution continues. Thus, C# gives you the choice of handling or ignoring overflow.
In addition to the arithmetic operators, integral-type to integral-type casts can cause overflow (for example, casting a long to an int) and are subject to checked or unchecked execution. Also note that bitwise operators and shift operators never cause overflow.
CLR 7.2.1 Operator precedence and associativity | Overloadable Operators | C# Keywords