# C# operators (C# reference)

C# provides a number of operators supported by the built-in types. For example, arithmetic operators perform arithmetic operations with numeric operands and Boolean logical operators perform logical operations with the bool operands. Certain operators can be overloaded. With operator overloading, you can specify the operator behavior for the operands of a user-defined type.

In an expression, operator precedence and associativity determine the order in which the operations are performed. You can use parentheses to change the order of evaluation imposed by operator precedence and associativity.

## Operator precedence

In an expression with multiple operators, the operators with higher precedence are evaluated before the operators with lower precedence. In the following example, the multiplication is performed first because it has higher precedence than addition:

```
var a = 2 + 2 * 2;
Console.WriteLine(a); // output: 6
```

Use parentheses to change the order of evaluation imposed by operator precedence:

```
var a = (2 + 2) * 2;
Console.WriteLine(a); // output: 8
```

The following table lists the C# operators starting with the highest precedence to the lowest. The operators within each row have the same precedence.

Operators | Category or name |
---|---|

x.y, x?.y, x?[y], f(x), a[i], x++, x--, new, typeof, checked, unchecked, default, nameof, delegate, sizeof, stackalloc, x->y | Primary |

+x, -x, !x, ~x, ++x, --x, (T)x, await, &x, *x, true and false | Unary |

x * y, x / y, x % y | Multiplicative |

x + y, x – y | Additive |

x << y, x >> y | Shift |

x < y, x > y, x <= y, x >= y, is, as | Relational and type-testing |

x == y, x != y | Equality |

`x & y` |
Boolean logical AND or bitwise logical AND |

`x ^ y` |
Boolean logical XOR or bitwise logical XOR |

`x | y` |
Boolean logical OR or bitwise logical OR |

x && y | Conditional AND |

x || y | Conditional OR |

x ?? y | Null-coalescing operator |

c ? t : f | Conditional operator |

x = y, x += y, x -= y, x *= y, x /= y, x %= y, x &= y, x |= y, x ^= y, x <<= y, x >>= y, => | Assignment and lambda declaration |

## Operator associativity

When operators have the same precedence, associativity of the operators determines the order in which the operations are performed:

*Left-associative*operators are evaluated in order from left to right. Except for the assignment operators and the null-coalescing operator`??`

, all binary operators are left-associative. For example,`a + b - c`

is evaluated as`(a + b) - c`

.*Right-associative*operators are evaluated in order from right to left. The assignment operators, the null-coalescing operator`??`

, and the conditional operator`?:`

are right-associative. For example,`x = y = z`

is evaluated as`x = (y = z)`

.

Use parentheses to change the order of evaluation imposed by operator associativity:

```
int a = 13 / 5 / 2;
int b = 13 / (5 / 2);
Console.WriteLine($"a = {a}, b = {b}"); // output: a = 1, b = 6
```

## Operand evaluation

Unrelated to operator precedence and associativity, operands in an expression are evaluated from left to right. The following examples demonstrate the order in which operators and operands are evaluated:

Expression | Order of evaluation |
---|---|

`a + b` |
a, b, + |

`a + b * c` |
a, b, c, *, + |

`a / b + c * d` |
a, b, /, c, d, *, + |

`a / (b + c) * d` |
a, b, c, +, /, d, * |

Typically, all operator operands are evaluated. Some operators evaluate operands conditionally. That is, the value of the first operand of such an operator defines if (or which) other operands should be evaluated. These operators are the conditional logical AND (`&&`

) and OR (`||`

) operators, the null-coalescing operator `??`

, the null-conditional operators `?.`

and `?[]`

, and the conditional operator `?:`

. See the description of each operator for more details.

## C# language specification

For more information, see the Operators section of the C# language specification.

## See also

## Feedback

Loading feedback...