Operators
C# supports most standard C operators and improves several capabilities to eliminate common coding errors.
Arithmetic operators (+, -, *, /, % and so forth) detect and disallow value overflow, to avoid unexpected results when working with numbers that become larger or smaller than the allowed value range of the type that stores them.
Primary Operators
These are the highest precedence operators.
| Operator | Purpose | Doc |
|---|---|---|
| x.y | Member access. | doc |
| x?.y | Null conditional member access. Returns null if the left-hand operand evaluates to null. |
doc |
| x?y | Null conditional index access. Returns null if the left-hand operand evaluates to null |
doc |
| f(x) | function invocation. | doc |
| a[x] | aggregate object indexing. | doc |
| 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). | doc |
| 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). | doc |
| new | type instantiation. | doc |
| typeof | returns the Type object representing the operand. | doc |
| checked | enables overflow checking for integer operations. | doc |
| unchecked | disables overflow checking for integer operations. This is the default compiler behavior. | doc |
| default(T) | produces the default value of type T. | |
| delegate | declares and returns a delegate instance. | |
| sizeof | returns the size in bytes of the type operand. | |
| -> | pointer dereferencing combined with member access. |
Unary Operators
These operators have higher precedence than the next section and lower precedence than the previous section.
| Operator | Purpose | Doc |
|---|---|---|
| +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. | doc |
| &x | address of. | |
| *x | dereferencing. |
Conditional operators
The conditional operator ? evaluates a Boolean expression and returns the result of one of the two expressions:
// variable = (condition) ? true : false ;
length = (people != null) ? 10 : null;
Assign null-conditionally, read: Assign null if target is null. Doesn't crash.
int? length = people?.Length; // null if people is null
The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result.
Console.WriteLine(a ?? b); // if a==null, write b.
The null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null.
myString ??= "Oli"; // If myString is null, it becomes "Oli"
Delegates need to be checked for null before invoking. This can be done with
public Action TileSelected;
TileSelected?.Invoke();