C# operators

C# (C Sharp) is a programming language that provides various operators to perform different operations on variables and values. Here are the commonly used operators in C#:

  1. Arithmetic Operators:
    • Addition (+): Adds two operands.
    • Subtraction (-): Subtracts the second operand from the first.
    • Multiplication (*): Multiplies two operands.
    • Division (/): Divides the first operand by the second.
    • Modulus (%): Returns the remainder after division.
  2. Assignment Operators:
    • Assignment (=): Assigns a value to a variable.
    • Addition assignment (+=): Adds the value of the right operand to the left operand and assigns the result to the left operand.
    • Subtraction assignment (-=): Subtracts the value of the right operand from the left operand and assigns the result to the left operand.
    • Multiplication assignment (*=): Multiplies the left operand by the value of the right operand and assigns the result to the left operand.
    • Division assignment (/=): Divides the left operand by the value of the right operand and assigns the result to the left operand.
    • Modulus assignment (%=): Computes the modulus of the left operand with the value of the right operand and assigns the result to the left operand.
  3. Comparison Operators:
    • Equal to (==): Checks if two operands are equal.
    • Not equal to (!=): Checks if two operands are not equal.
    • Greater than (>): Checks if the left operand is greater than the right operand.
    • Less than (<): Checks if the left operand is less than the right operand.
    • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
    • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
  4. Logical Operators:
    • Logical AND (&&): Returns true if both operands are true.
    • Logical OR (||): Returns true if at least one of the operands is true.
    • Logical NOT (!): Reverses the logical state of the operand.
  5. Bitwise Operators:
    • Bitwise AND (&): Performs a bitwise AND operation between two operands.
    • Bitwise OR (|): Performs a bitwise OR operation between two operands.
    • Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation between two operands.
    • Bitwise NOT (~): Performs a bitwise NOT (one’s complement) operation on a single operand.
    • Left shift (<<): Shifts the bits of the left operand to the left by a specified number of positions.
    • Right shift (>>): Shifts the bits of the left operand to the right by a specified number of positions.

These are some of the fundamental operators in C#. Additionally, there are also conditional operators (ternary operator) and other specialized operators like the null coalescing operator (??) and the is/as operators for type checking and casting, among others.

Precedence of Operators in C# :

In C#, operators have different levels of precedence, which determines the order in which they are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. Here’s a list of C# operators arranged in order of their precedence, from highest to lowest:

  1. Postfix operators:
    • () Method call
    • [] Array indexing
    • . Member access
    • ++ Post-increment
    • — Post-decrement
  2. Unary operators:
      • Unary plus
      • Unary minus
    • ! Logical NOT
    • ~ Bitwise NOT
    • ++ Pre-increment
    • — Pre-decrement
    • (type) Cast
  3. Multiplicative operators:
      • Multiplication
    • / Division
    • % Modulus
  4. Additive operators:
      • Addition
      • Subtraction
  5. Shift operators:
    • << Left shift
    • Right shift

  6. Relational and type testing operators:
    • < Less than
    • Greater than

    • <= Less than or equal to
    • = Greater than or equal to

    • is Type testing operator
    • as Type casting operator
  7. Equality operators:
    • == Equal to
    • != Not equal to
  8. Bitwise AND operator:
    • & Bitwise AND
  9. Bitwise XOR operator:
    • ^ Bitwise XOR
  10. Bitwise OR operator:
    • | Bitwise OR
  11. Conditional AND operator:
    • && Conditional AND
  12. Conditional OR operator:
    • || Conditional OR
  13. Null-coalescing operator:
    • ?? Null-coalescing
  14. Conditional operator:
    • ?: Ternary conditional
  15. Assignment and lambda operators:
    • = Assignment
    • += Addition assignment
    • -= Subtraction assignment
    • *= Multiplication assignment
    • /= Division assignment
    • %= Modulus assignment
    • &= Bitwise AND assignment
    • |= Bitwise OR assignment
    • ^= Bitwise XOR assignment
    • <<= Left shift assignment
    • = Right shift assignment

    • => Lambda operator

It’s important to note that parentheses can be used to override the default precedence and explicitly define the order of evaluation in an expression.