Operators Table
The following table describes the many operators in C#, their ordering (indicated by their category), their associativity, and whether they are overloadable or not.
Name | Syntax | Description | Category | Overloadable? | Associativity | Arity |
---|---|---|---|---|---|---|
Member Access | x.y |
Access members of an instance, type, namespace, etc. | Primary | No | Left-to-Right | 2 |
Null-Conditional Member Access | x?.y |
Member access with a null check first | Primary | No | Left-to-Right | 2 |
Method Invocation | x(y) |
Invoking/calling a method | Primary | No | Left-to-Right | 2 |
Aggregate Object Indexing | x[y] |
Accessing items in a collection | Primary | Indexers | Left-to-Right | 2 |
Null-Conditional Indexing | x?[y] |
Collection access with a null check first | Primary | Indexers | Left-to-Right | 2 |
Postfix Increment | x++ |
Adds 1 to a variable (original value returned) | Primary | Yes | Left-to-Right | 1 |
Postfix Decrement | x-- |
Subtracts 1 from a variable (original value returned) | Primary | Yes | Left-to-Right | 1 |
Type Instantiation | new |
Creates new objects by invoking a constructor | Primary | No | Right-to-Left | 1 |
Typeof | typeof(T) |
Produces a Type object from the name of a type |
Primary | No | Right-to-Left | 1 |
Checked | checked |
Marks a checked context | Primary | No | Right-to-Left | 1 |
Unchecked | unchecked |
Marks an unchecked context | Primary | No | Right-to-Left | 1 |
Default | default |
Provides the default value of a given type | Primary | No | Right-to-Left | 1 |
Delegate | delegate |
Defines delegate types or makes new delegate instances | Primary | No | Right-to-Left | 1 |
Sizeof | sizeof(T) |
Produces the size of a given type | Primary | No | Right-to-Left | 1 |
Pointer Dereference | x->y |
Member access through a pointer (unsafe context) | Primary | No | Left-to-Right | 2 |
switch | switch |
Evaluates a subexpression based on pattern matching | switch expression |
No | Right-to-Left | 1 |
Unary Minus/Numeric Negation | -x |
Produces the negative of the operand | Unary | Yes | Right-to-Left | 1 |
Logical Negation | !x |
Produces the Boolean inverse of the operand | Unary | Yes | Right-to-Left | 1 |
Bitwise Complement | ~x |
Produces the bitwise complement of the operand | Unary | Yes | Right-to-Left | 1 |
Prefix Increment | ++x |
Adds 1 to a variable (incremented value returned) | Unary | Yes | Right-to-Left | 1 |
Prefix Decrement | --x |
Subtracts 1 from a variable (incremented value returned) | Unary | Yes | Right-to-Left | 1 |
Type Casting | (T)x |
Specifies a cast or type conversion | Unary | User-defined conversion | Right-to-Left | 1 |
Await | await |
Awaits asynchronous tasks | Unary | No | Right-to-Left | 1 |
Address Of | &x |
Produces the address of a variable (unsafe context) | Unary | No | Right-to-Left | 1 |
Dereferencing/Indirection | *x |
Declare pointer types and dereference pointers | Unary | No | Right-to-Left | 1 |
Range | x..y |
accessing a range of values in a collection | Binary | No | ? | 1 |
switch | switch |
Evaluates a subexpression based on pattern matching | switch expression |
No | Right-to-Left | 1 |
with | with |
Produces a copy of a record with specific modifications | with expression |
No | ? | 1 |
Multiplication | x * y |
Multiplies two values | Multiplicative | Yes | Left-to-Right | 2 |
Division | x / y |
Divides the first value by the second | Multiplicative | Yes | Left-to-Right | 2 |
Remainder/Modulus | x % y |
Produces the remainder of a division operation | Multiplicative | Yes | Left-to-Right | 2 |
Addition | x + y |
Adds two values | Additive | Yes | Left-to-Right | 2 |
Subtraction | x - y |
Subtracts the second value from the first | Additive | Yes | Left-to-Right | 2 |
Left Shift | x << y |
Bitwise shifts a value a number of bits leftward | Shift | Yes | Left-to-Right | 2 |
Right Shift | x >> y |
Bitwise shifts a value a number of bits rightward | Shift | Yes | Left-to-Right | 2 |
Less Than | x < y |
Returns whether x is less than y |
Relational and Type Testing | Yes | Left-to-Right | 2 |
Greater Than | x > y |
Returns whether x is greater than y |
Relational and Type Testing | Yes | Left-to-Right | 2 |
Less Than Or Equal | x <= y |
Returns whether x is less than or equal to y |
Relational and Type Testing | Yes | Left-to-Right | 2 |
Greater Than Or Equal | x >= y |
Returns whether x is greater than or equal to y |
Relational and Type Testing | Yes | Left-to-Right | 2 |
Is | is |
Determines if a value is a specific type or matches a pattern | Relational and Type Testing | No | Right-to-Left | 2 |
As | as |
Type conversion. Results in null instead of exceptions | Relational and Type Testing | No | Right-to-Left | 2 |
Equality | x == y |
Returns whether x equals y |
Equality | Yes | Left-to-Right | 2 |
Inequality | x != y |
Returns whether x does not equal y |
Equality | Yes | Left-to-Right | 2 |
Logical AND | x & y |
Bitwise or Boolean “and” operation | Logical AND | Yes | Left-to-Right | 2 |
Logical XOR | x ^ y |
Bitwise or Boolean “exclusive or” operation | Logical XOR | Yes | Left-to-Right | 2 |
Logical OR | x | y |
Bitwise or Boolean “or” operation | Logical OR | Yes | Left-to-Right | 2 |
Conditional AND | x && y |
Returns whether x and y are both true. |
Conditional AND | No | Left-to-Right | 2 |
Conditional OR | x || y |
Returns whether x is true or y is true. |
Conditional OR | No | Left-to-Right | 2 |
Null Coalescing | x ?? y |
Returns x unless it is null , otherwise returns y |
Null Coalescing | No | Right-to-Left | 2 |
Conditional/Ternary | t ? x : y |
If t is true, results in x , otherwise, y |
Conditional | No | Right-to-Left | 2 |
Assignment | x = y |
Assigns the value y to x |
Assignment and Lambda | No | Right-to-Left | 2 |
Addition Assignment | x += y |
Shorthand for x = x + y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
Subtraction Assignment | x -= y |
Shorthand for x = x - y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
Multiplication Assignment | x *= y |
Shorthand for x = x * y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
Division Assignment | x /= y |
Shorthand for x = x / y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
Remainder Assignment | x %= y |
Shorthand for x = x % y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
AND Assignment | x &= y |
Shorthand for x = x & y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
OR Assignment | x |= y |
Shorthand for x = x | y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
XOR Assignment | x ^= y |
Shorthand for x = x ^ y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
Left Shift Assignment | x <<= y |
Shorthand for x = x << y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
Right Shift Assignment | x >>= y |
Shorthand for x = x >> y |
Assignment and Lambda | Indirectly | Right-to-Left | 2 |
Null-Coalescing Assignment | x ??= y |
Shorthand for x = x ?? y |
Assignment and Lambda | No | Right-to-Left | 2 |
Lambda Declaration | => |
Introduces a lambda expression or statement | Assignment and Lambda | No | Right-to-Left | 2 |