C# Operators

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 of the division of the first operand by the second.
                                                    
                                                    int x = 10;
int y = 5;

int addition = x + y;       // addition = 10 + 5 = 15
int subtraction = x - y;    // subtraction = 10 - 5 = 5
int multiplication = x * y; // multiplication = 10 * 5 = 50
int division = x / y;       // division = 10 / 5 = 2
int modulus = x % y;        // modulus = 10 % 5 = 0
                                                    
                                                

Comparison Operators:

  • == (Equality): Returns true if the operands are equal; otherwise, false.
  • != (Inequality): Returns true if the operands are not equal; otherwise, false.
  • < (Less than): Returns true if the first operand is less than the second; otherwise, false.
  • > (Greater than): Returns true if the first operand is greater than the second; otherwise, false.
  • <= (Less than or equal to): Returns true if the first operand is less than or equal to the second; otherwise, false.
  • >= (Greater than or equal to): Returns true if the first operand is greater than or equal to the second; otherwise, false.
                                                    
                                                    int a = 10;
int b = 20;

bool isEqual = (a == b);     // isEqual = false
bool isNotEqual = (a != b);  // isNotEqual = true
bool isGreaterThan = (a > b); // isGreaterThan = false
bool isLessThan = (a < b);    // isLessThan = true
bool isGreaterOrEqual = (a >= b); // isGreaterOrEqual = false
bool isLessOrEqual = (a <= b);    // isLessOrEqual = true
                                                    
                                                

Logical Operators:

  • && (Logical AND): Returns true if both operands are true; otherwise, false.
  • || (Logical OR): Returns true if either operand is true; otherwise, false.
  • ! (Logical NOT): Returns the opposite of the operand's value; if the operand is true, ! returns false, and vice versa.
                                                    
                                                    bool condition1 = true;
bool condition2 = false;

bool logicalAnd = (condition1 && condition2); // logicalAnd = false
bool logicalOr = (condition1 || condition2);  // logicalOr = true
bool logicalNot = !condition1;                // logicalNot = false
                                                    
                                                

Assignment Operators:

  • = (Assignment): Interchanges the value for right operant to left operand.
  • += (Addition assignment): Performs the operation of addition and the obtained value to the left operand is kept and the right operand is skipped.
  • -= (Subtraction assignment): Sums up the value of the left operand and the right operand and returns their product to the left operand.
  • *= (Multiplication assignment): More to the left, multiplies operands by the value from the right side. The output is assigned on the opposite (left) side.
  • /= (Division assignment): Splits the value on the left by an occurrence of the value on the right and assigns the outcome to the left operand.
  • %= (Modulus assignment): Performs a modulo operation that is the modulus of the left operand divided by the value of the right operand, and returns it to the left operand.
                                                    
                                                    int num = 10;
num += 5;  // num = num + 5 = 15
num -= 3;  // num = num - 3 = 12
num *= 2;  // num = num * 2 = 24
num /= 4;  // num = num / 4 = 6
num %= 2;  // num = num % 2 = 0