In C#, an operator is a program element that is applied to one or more operands in an expression or statement. Operators that take one operand, such as the increment operator (++) or new, are referred to as unary operators. Operators that take two operands, such as arithmetic operators (+,-,*,/), are referred to as binary operators. One operator, the conditional operator (?:), takes three operands and is the sole ternary operator in C#.
Parameter | Details |
---|---|
operatorSymbol | The operator being overloaded, e.g. +, -, /, * |
OperandType | The type that will be returned by the overloaded operator. |
operand1 | The first operand to be used in performing the operation. |
operand2 | The second operand to be used in performing the operation, when doing binary operations. |
statements | Optional code needed to perform the operation before returning the result. |
All operators are defined as static methods
and they are not virtual
and they are not inherited.
All operators have a particular "precedence" depending on which group the operator falls in (operators of the same group have equal precedence). Meaning some operators will be applied before others. What follows is a list of groups (containing their respective operators) ordered by precedence (highest first):
Primary Operators
a.b
- Member access.a?.b
- Null conditional member access.->
- Pointer dereferencing combined with member access.f(x)
- Function invocation.a[x]
- Indexer.a?[x]
- Null conditional indexer.x++
- Postfix increment.x--
- Postfix decrement.new
- Type instantiation.default(T)
- Returns the default initialized value of type T
.typeof
- Returns the Type
object of the operand.checked
- Enables numeric overflow checking.unchecked
- Disables numeric overflow checking.delegate
- Declares and returns a delegate instance.sizeof
- Returns the size in bytes of the type operand.Unary Operators
+x
- Returns x
.-x
- Numeric negation.!x
- Logical negation.~x
- Bitwise complement/declares destructors.++x
- Prefix increment.--x
- Prefix decrement.(T)x
- Type casting.await
- Awaits a Task
.&x
- Returns the address (pointer) of x
.*x
- Pointer dereferencing.Multiplicative Operators
x * y
- Multiplication.x / y
- Division.x % y
- Modulus.Additive Operators
x + y
- Addition.x – y
- Subtraction.Bitwise Shift Operators
x << y
- Shift bits left.x >> y
- Shift bits right.Relational/Type-testing Operators
x < y
- Less than.x > y
- Greater than.x <= y
- Less than or equal to.x >= y
- Greater than or equal to.is
- Type compatibility.as
- Type conversion.Equality Operators
x == y
- Equality.x != y
- Not equal.Logical AND Operator
x & y
- Logical/bitwise AND.Logical XOR Operator
x ^ y
- Logical/bitwise XOR.Logical OR Operator
x | y
- Logical/bitwise OR.Conditional AND Operator
x && y
- Short-circuiting logical AND.Conditional OR Operator
x || y
- Short-circuiting logical OR.Null-coalescing Operator
x ?? y
- Returns x
if it is not null; otherwise, returns y
.Conditional Operator
x ? y : z
- Evaluates/returns y
if x
is true; otherwise, evaluates z
.Related Content