38

C# Operators types

1. Arithmetic Operators

2. Comparison Operators

3. Logical Operators

4. Assignment Operators

5. Increment and Decrement Operators

6. Conditional Operator (Ternary Operator)

1. Arithmetic Operators

  • Addition (+)
    • Adds two operands
    • Example: int sum = a + b;
  • Subtraction (-)
    • Subtracts second operand from the first
    • Example: int difference = a – b;
  • Multiplication (*)
    • Multiplies two operands
    • Example: int product = a * b;
  • Division (/)
    • Divides numerator by denominator
    • Example: int quotient = a / b;
  • Modulus (%)
    • Returns remainder of division
    • Example: int remainder = a % b;

2. Comparison Operators

Equal to (==)

  • Checks if two operands are equal
  • Example: bool isEqual = a == b;

Not equal to (!=)

  • Checks if two operands are not equal
  • Example: bool isNotEqual = a != b;

Greater than (>)

  • Checks if the left operand is greater than  the right
  • Example: bool isGreater = a > b;

Less than (<)

  • Checks if the left operand is less than the right
  • Example: bool isLess = a < b;

Greater than or equal to (>=)

  • Checks if the left operand is greater than or equal to the right
  • Example: bool isGreaterOrEqual = a >= b;

Less than or equal to (<=)

  • Checks if the left operand is less than or equal to the right
  • Example: bool isLessOrEqual = a <= b;

3. Logical Operators

Logical AND (&&)

  • Returns true if both operands are true
  • Example: bool result = a && b;

Logical OR (||)

  • Returns true if either operand is true
  • Example: bool result = a || b;

Logical NOT (!)

  • Reverses the logical state of its operand
  • Example: bool result = !a;

4. Assignment Operators

Assignment (=)

  • Assigns right operand to left operand
  • Example: a = b;

Addition assignment (+=)

  • Adds right operand to left operand and assigns the result to the left operand
  • Example: a += b;

Subtraction assignment (-=)

  • Subtracts right operand from left operand and assigns the result to the left operand
  • Example: a -= b;

Multiplication assignment (*=)

  • Multiplies left operand by right operand and assigns the result to the left operand
  • Example: a *= b;

Division assignment (/=)

  • Divides left operand by right operand and assigns the result to the left operand
  • Example: a /= b;

Modulus assignment (%=)

  • Calculates modulus using two operands and assigns the result to the left operand
  • Example: a %= b;

5. Increment and Decrement Operators

  • Increment (++)
  • Increases an integer value by one
  • Example: a++;
  • Decrement (–)
  • Decreases an integer value by one
  • Example: a–;

6. Conditional Operator (Ternary Operator)

  • Conditional (?:)
  • Evaluates a boolean expression and returns the result of one of two expressions
  • Example: string result = (a == b) ? “Equal” : “Not Equal”;

Write A Comment