Sure! In C#, operators are symbols that perform operations on operands, such as variables, constants, and expressions. Here are some common operators in C# explained with examples:
Example
int a = 10;
int b = 5;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b; // Modulus (remainder after division)
true
or false
) based on the comparison.Example
int x = 10;
int y = 5;
bool isEqual = (x == y); // Equal to
bool isNotEqual = (x != y); // Not equal to
bool isGreater = (x > y); // Greater than
bool isLess = (x < y); // Less than
bool isGreaterOrEqual = (x >= y); // Greater than or equal to
bool isLessOrEqual = (x <= y); // Less than or equal to
Example
bool p = true;
bool q = false;
bool resultAnd = (p && q); // Logical AND
bool resultOr = (p || q); // Logical OR
bool resultNot = !p; // Logical NOT
Example
int num = 10;
num += 5; // Equivalent to: num = num + 5;
num -= 2; // Equivalent to: num = num - 2;
num *= 3; // Equivalent to: num = num * 3;
num /= 2; // Equivalent to: num = num / 2;
num %= 4; // Equivalent to: num = num % 4;
Example
int count = 0;
count++; // Increment by 1
count--; // Decrement by 1
These are some of the basic operators in C#. Understanding and mastering these operators will be essential for writing C# code effectively.