# C# operators (C# reference)

C# provides a number of predefined operators supported by the built-in types. For example, arithmetic operators perform arithmetic operations with operands of built-in numeric types and Boolean logical operators perform logical operations with the bool operands.

A user-defined type can overload certain operators to define the corresponding behavior for the operands of that type. For more information, see the operator keyword article.

The following sections list the C# operators starting with the highest precedence to the lowest. The operators within each section share the same precedence level.

## Primary operators

These are the highest precedence operators.

x.y – member access.

x?.y – null conditional member access. Returns `null`

if the left-hand operand evaluates to `null`

.

x?[y] - null conditional array element or type indexer access. Returns `null`

if the left-hand operand evaluates to `null`

.

f(x) – method call or delegate invocation.

a[x] – array element or type indexer access.

x++ – postfix increment. Returns the value of x and then updates the storage location with the value of x that is one greater (typically adds the integer 1).

x-- – postfix decrement. Returns the value of x and then updates the storage location with the value of x that is one less (typically subtracts the integer 1).

new – type instantiation.

typeof – returns the Type object representing the operand.

checked – enables overflow checking for integer operations.

unchecked – disables overflow checking for integer operations. This is the default compiler behavior.

default(T) – produces the default value of type T.

nameof - obtains the simple (unqualified) name of a variable, type, or member as a constant string.

delegate – declares and returns a delegate instance.

sizeof – returns the size in bytes of the type operand.

stackalloc - allocates a block of memory on the stack.

-> – pointer indirection combined with member access.

## Unary operators

These operators have higher precedence than the next section and lower precedence than the previous section.

+x – returns the value of x.

-x – numeric negation.

!x – logical negation.

~x – bitwise complement.

++x – prefix increment. Returns the value of x after updating the storage location with the value of x that is one greater (typically adds the integer 1).

--x – prefix decrement. Returns the value of x after updating the storage location with the value of x that is one less (typically subtracts the integer 1).

(T)x – type casting.

await – awaits a `Task`

.

&x – address of a variable.

*x – pointer indirection, or dereference.

true operator - returns the bool value `true`

to indicate that an operand is definitely true.

false operator - returns the bool value `true`

to indicate that an operand is definitely false.

## Multiplicative operators

These operators have higher precedence than the next section and lower precedence than the previous section.

x * y – multiplication.

x / y – division. If the operands are integers, the result is an integer truncated toward zero (for example, `-7 / 2 is -3`

).

x % y – remainder. If the operands are integers, this returns the remainder of dividing x by y. If `q = x / y`

and `r = x % y`

, then `x = q * y + r`

.

## Additive operators

These operators have higher precedence than the next section and lower precedence than the previous section.

x + y – addition.

x – y – subtraction.

## Shift operators

x << y – shift bits left and fill with zero on the right.

x >> y – shift bits right. If the left operand is `int`

or `long`

, then left bits are filled with the sign bit. If the left operand is `uint`

or `ulong`

, then left bits are filled with zero.

## Relational and type-testing operators

x < y – less than (true if x is less than y).

x > y – greater than (true if x is greater than y).

x <= y – less than or equal to.

x >= y – greater than or equal to.

is – type compatibility. Returns `true`

if the evaluated left operand can be cast to the type specified by the right operand.

as – type conversion. Returns the left operand cast to the type specified by the right operand, but `as`

returns `null`

where `(T)x`

would throw an exception.

## Equality operators

x == y – equality. By default, for reference types other than `string`

, this returns reference equality (identity test). However, types can overload `==`

, so if your intent is to test identity, it is best to use the `ReferenceEquals`

method on `object`

.

x != y – not equal. See comment for `==`

. If a type overloads `==`

, then it must overload `!=`

.

## Logical AND operator

This operator has higher precedence than the next section and lower precedence than the previous section.

`x & y`

– logical AND for the `bool`

operands or bitwise logical AND for the operands of the integral types.

## Logical XOR operator

This operator has higher precedence than the next section and lower precedence than the previous section.

`x ^ y`

– logical XOR for the `bool`

operands or bitwise logical XOR for the operands of the integral types.

## Logical OR operator

This operator has higher precedence than the next section and lower precedence than the previous section.

`x | y`

– logical OR for the `bool`

operands or bitwise logical OR for the operands of the integral types.

## Conditional AND operator

x && y – logical AND. If `x`

evaluates to `false`

, then `y`

is not evaluated.

## Conditional OR operator

x || y – logical OR. If `x`

evaluates to `true`

, then `y`

is not evaluated.

## Null-coalescing operator

x ?? y – returns `x`

if it is non-`null`

; otherwise, returns `y`

.

## Conditional operator

t ? x : y – if test `t`

evaluates to true, then evaluate and return `x`

; otherwise, evaluate and return `y`

.

## Assignment and lambda operators

x = y – assignment.

x += y – increment. Add the value of `y`

to the value of `x`

, store the result in `x`

, and return the new value. If `x`

designates an event, then `y`

must be an appropriate method that C# adds as an event handler.

x -= y – decrement. Subtract the value of `y`

from the value of `x`

, store the result in `x`

, and return the new value. If `x`

designates an event, then `y`

must be an appropriate method that C# removes as an event handler.

x *= y – multiplication assignment. Multiply the value of `y`

to the value of `x`

, store the result in `x`

, and return the new value.

x /= y – division assignment. Divide the value of `x`

by the value of `y`

, store the result in `x`

, and return the new value.

x %= y – remainder assignment. Divide the value of `x`

by the value of `y`

, store the remainder in `x`

, and return the new value.

x &= y – AND assignment. AND the value of `y`

with the value of `x`

, store the result in `x`

, and return the new value.

x |= y – OR assignment. OR the value of `y`

with the value of `x`

, store the result in `x`

, and return the new value.

x ^= y – XOR assignment. XOR the value of `y`

with the value of `x`

, store the result in `x`

, and return the new value.

x <<= y – left-shift assignment. Shift the value of `x`

left by `y`

places, store the result in `x`

, and return the new value.

x >>= y – right-shift assignment. Shift the value of `x`

right by `y`

places, store the result in `x`

, and return the new value.

=> – lambda declaration.

## See also

## Feedback

Loading feedback...