Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

C# Operators

http://msdn.microsoft.com/en-us/library/6a71f45d

C# Operators
Visual Studio 2010 10 out of 14 rated this helpful - Rate this topic C# provides a large set of operators, which are symbols that specify which operations to perform in an expression. Operations on integral types such as ==, !=, <, >, <=, >=, binary +, binary -, ^, &, |, ~, ++, --, and sizeof() are generally allowed on enumerations. In addition, many operators can be overloaded by the user, thus changing their meaning when applied to a user-defined type. The following table lists the C# operators grouped in order of precedence. Operators within each group have equal precedence.

Operator category
Primary

Operators
x.y f(x) a[x] x++ x-new typeof checked unchecked default(T) delegate ->

Unary

+ ! ~ ++x --x (T)x true

1 of 4

5/25/2012 11:50 AM

C# Operators

http://msdn.microsoft.com/en-us/library/6a71f45d

false & sizeof Multiplicative * / % Additive + Shift << >> Relational and type testing < > <= >= is as Equality == != Logical AND Logical XOR Logical OR Conditional AND Conditional OR Null-coalescing Conditional Assignment and lambda expression & ^ | && || ?? ?: = +=

2 of 4

5/25/2012 11:50 AM

C# Operators

http://msdn.microsoft.com/en-us/library/6a71f45d

-= *= /= %= &= |= ^= <<= >>= => Lambda Operator

Arithmetic Overflow
The arithmetic operators (+, -, *, /) can produce results that are outside the range of possible values for the numeric type involved. You should refer to the section on a particular operator for details, but in general: Integer arithmetic overflow either throws an OverflowException or discards the most significant bits of the result. Integer division by zero always throws a DivideByZeroException. Floating-point arithmetic overflow or division by zero never throws an exception, because floating-point types are based on IEEE 754 and so have provisions for representing infinity and NaN (Not a Number). Decimal arithmetic overflow always throws an OverflowException. Decimal division by zero always throws a DivideByZeroException. 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. However, bitwise operators and shift operators never cause overflow.

See Also
Reference Overloadable Operators (C# Programming Guide) C# Keywords Concepts C# Programming Guide Other Resources

3 of 4

5/25/2012 11:50 AM

C# Operators C# Reference Visual C#

http://msdn.microsoft.com/en-us/library/6a71f45d

Did you find this helpful?

Yes

No

Community Content
Precedence needs to be explained better
$0$0 According to this page the following would evaluate to 8 since multiplication is in the third precedence group. But this evaluates to 4. I think precedence needs to be explained better on this page. $0$0 $0 $0Int32 x = 1;$0 $0Int32 y = 5;$0 $0Int32 z = x++ * --y;$0 $0MessageBox.Show(z.ToString());$0 $0$0 $0 $0@Pam: Depends on weather the ++ (or --) operator is before or after the operand. Putting the ++ before the operand causes the operand to be incremented before it is used. Putting the ++ after the operand (as in your example) causes the operand to be incremented after it is used.$0 $0$0 $0 $0z = x++ * --y; (evaluated as z = 1 * 4)$0 $0z = ++x * --y; (evaluated as z = 2 * 4)$0 $0z = x++ * y--; (evaluated as z = 1 * 5)$0 $0z = ++x * y--; (evaluated as z = 2 * 5)$0 1/30/2012 Old Tymer 1/15/2012 Pam

2012 Microsoft. All rights reserved.

4 of 4

5/25/2012 11:50 AM

You might also like