C# Language Operators

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

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#.

Syntax

  • public static OperandType operator operatorSymbol(OperandType operand1)
  • public static OperandType operator operatorSymbol(OperandType operand1, OperandType2 operand2)

Parameters

ParameterDetails
operatorSymbolThe operator being overloaded, e.g. +, -, /, *
OperandTypeThe type that will be returned by the overloaded operator.
operand1The first operand to be used in performing the operation.
operand2The second operand to be used in performing the operation, when doing binary operations.
statementsOptional code needed to perform the operation before returning the result.

Remarks

All operators are defined as static methods and they are not virtual and they are not inherited.

Operator Precedence

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



Got any C# Language Question?