Operators

Operators in C# are symbols or keywords that perform various operations on operands (variables, constants, or expressions) and produce a result. C# supports a wide range of operators for arithmetic, assignment, comparison, logical operations, and more. Here are some commonly used operators in C#:

  1. Arithmetic Operators:

  • Addition: + (e.g., int sum = a + b;)
  • Subtraction: – (e.g., int difference = a – b;)
  • Multiplication: * (e.g., int product = a * b;)
  • Division: / (e.g., double quotient = a / b;)
  • Modulus (Remainder): % (e.g., int remainder = a % b;)
  • Increment: ++ (e.g., a++;)
  • Decrement: — (e.g., b–;)
  • Assignment Operators:
  • Assignment: = (e.g., int x = 10;)
  • Compound Assignment: +=, -=, *=, /=, %= (e.g., a += b; is equivalent to a = a + b;)
  1. Comparison Operators:

 

  • Equal to: == (e.g., if (a == b) { /* do something */ })
  • Not equal to: != (e.g., if (a != b) { /* do something */ })
  • Greater than: > (e.g., if (a > b) { /* do something */ })
  • Less than: < (e.g., if (a < b) { /* do something */ })
  • Greater than or equal to: >= (e.g., if (a >= b) { /* do something */ })
  • Less than or equal to: <= (e.g., if (a <= b) { /* do something */ })
  1. Logical Operators:

 

  • Logical AND: && (e.g., if (condition1 && condition2) { /* do something */ })
  • Logical OR: || (e.g., if (condition1 || condition2) { /* do something */ })
  • Logical NOT: ! (e.g., if (!condition) { /* do something */ })
  1. Bitwise Operators:

  • Bitwise AND: & (e.g., result = a & b;)
  • Bitwise OR: | (e.g., result = a | b;)
  • Bitwise XOR: ^ (e.g., result = a ^ b;)
  • Bitwise NOT: ~ (e.g., result = ~a;)
  • Left Shift: << (e.g., result = a << b;)
  • Right Shift: >> (e.g., result = a >> b;)