Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
C# Operators
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
C# Operators

Updated: July 2008

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

Operators

Primary

x.y

f(x)

a[x]

x++

x--

new

typeof

checked

unchecked

->

Unary

+

-

!

~

++x

--x

(T)x

true

false

&

sizeof

Multiplicative

*

/

%

Additive

+

-

Shift

<<

>>

Relational and type testing

<

>

<=

>=

is

as

Equality

==

!=

Logical AND

&

Logical XOR

^

Logical OR

|

Conditional AND

&&

Conditional OR

||

Conditional

?:

Assignment

=

+=

-=

*=

/=

%=

&=

|=

^=

<<=

>>=

Null-coalescing

??

Lambda

=> Operator (C# Reference)

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.

Date

History

Reason

July 2008

Moved ?? operator out of assignment category per customer suggestion.

Content bug fix.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Incorrect precedences for ?? and =>      Rising   |   Edit   |   Show History
Null-coalescing (??) should be below Conditional OR (||) and above Conditional (?:). Lambda (=>) should be listed with Assignment, not below. See §7.2.1 of the C# Language Specification, Version 3.0.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker