C# Operators

Introduction

Operators in C# are special symbols that perform operations on variables and values. C# provides a rich set of built-in operators, including arithmetic, relational, logical, bitwise, assignment, and other miscellaneous operators. Understanding these operators is essential for writing efficient and effective C# code.

Categories of Operators

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Miscellaneous Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.

  • 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 when the first operand is divided by the second.
  • Increment (++): Increases an integer value by one.
  • Decrement (–): Decreases an integer value by one.

Examples

int a = 10;
int b = 3;

int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1

a++; // 11
b--; // 2

Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Difference: {difference}");
Console.WriteLine($"Product: {product}");
Console.WriteLine($"Quotient: {quotient}");
Console.WriteLine($"Remainder: {remainder}");
Console.WriteLine($"Incremented a: {a}");
Console.WriteLine($"Decremented b: {b}");

2. Relational Operators

Relational operators are used to compare two values. They return a Boolean value (true or false).

  • Equal to (==): Checks if two operands are equal.
  • Not equal to (!=): Checks if two operands are not equal.
  • Greater than (>): Checks if the first operand is greater than the second.
  • Less than (<): Checks if the first operand is less than the second.
  • Greater than or equal to (>=): Checks if the first operand is greater than or equal to the second.
  • Less than or equal to (<=): Checks if the first operand is less than or equal to the second.

Examples

int x = 5;
int y = 10;

bool isEqual = x == y; // false
bool isNotEqual = x != y; // true
bool isGreater = x > y; // false
bool isLess = x < y; // true
bool isGreaterOrEqual = x >= y; // false
bool isLessOrEqual = x <= y; // true

Console.WriteLine($"isEqual: {isEqual}");
Console.WriteLine($"isNotEqual: {isNotEqual}");
Console.WriteLine($"isGreater: {isGreater}");
Console.WriteLine($"isLess: {isLess}");
Console.WriteLine($"isGreaterOrEqual: {isGreaterOrEqual}");
Console.WriteLine($"isLessOrEqual: {isLessOrEqual}");

3. Logical Operators

Logical operators are used to combine multiple Boolean expressions.

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if at least one of the operands is true.
  • Logical NOT (!): Inverts the value of the operand.

Examples

bool a = true;
bool b = false;

bool andResult = a && b; // false
bool orResult = a || b; // true
bool notResult = !a; // false

Console.WriteLine($"AND: {andResult}");
Console.WriteLine($"OR: {orResult}");
Console.WriteLine($"NOT: {notResult}");

4. Bitwise Operators

Bitwise operators perform bit-level operations on integer types.

  • Bitwise AND (&): Performs a bitwise AND operation.
  • Bitwise OR (|): Performs a bitwise OR operation.
  • Bitwise XOR (^): Performs a bitwise XOR operation.
  • Bitwise Complement (~): Inverts each bit.
  • Left Shift (<<): Shifts bits to the left.
  • Right Shift (>>): Shifts bits to the right.

Examples

int a = 5; // 0101 in binary
int b = 3; // 0011 in binary

int andResult = a & b; // 0001 (1 in decimal)
int orResult = a | b; // 0111 (7 in decimal)
int xorResult = a ^ b; // 0110 (6 in decimal)
int complement = ~a; // 1111...1010 (-6 in decimal, two's complement)
int leftShift = a << 1; // 1010 (10 in decimal)
int rightShift = a >> 1; // 0010 (2 in decimal)

Console.WriteLine($"Bitwise AND: {andResult}");
Console.WriteLine($"Bitwise OR: {orResult}");
Console.WriteLine($"Bitwise XOR: {xorResult}");
Console.WriteLine($"Bitwise Complement: {complement}");
Console.WriteLine($"Left Shift: {leftShift}");
Console.WriteLine($"Right Shift: {rightShift}");

5. Assignment Operators

Assignment operators are used to assign values to variables. They include the simple assignment operator and compound assignment operators that perform an operation and assignment in one step.

  • Simple Assignment (=): Assigns the value of the right operand to the left operand.
  • Add and Assign (+=): Adds the right operand to the left operand and assigns the result to the left operand.
  • Subtract and Assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
  • Multiply and Assign (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
  • Divide and Assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.
  • Modulus and Assign (%=): Takes the modulus using the two operands and assigns the result to the left operand.
  • Bitwise AND and Assign (&=): Performs a bitwise AND operation on the two operands and assigns the result to the left operand.
  • Bitwise OR and Assign (|=): Performs a bitwise OR operation on the two operands and assigns the result to the left operand.
  • Bitwise XOR and Assign (^=): Performs a bitwise XOR operation on the two operands and assigns the result to the left operand.
  • Left Shift and Assign (<<=): Shifts the bits of the left operand left by the number of positions specified by the right operand and assigns the result to the left operand.
  • Right Shift and Assign (>>=): Shifts the bits of the left operand right by the number of positions specified by the right operand and assigns the result to the left operand.

Examples

int a = 10;
a += 2;  // Equivalent to a = a + 2; // a is now 12
a *= 3;  // Equivalent to a = a * 3; // a is now 36
a -= 4;  // Equivalent to a = a - 4; // a is now 32
a /= 2;  // Equivalent to a = a / 2; // a is now 16
a %= 5;  // Equivalent to a = a % 5; // a is now 1

Console.WriteLine($"a: {a}");

6. Miscellaneous Operators

Miscellaneous operators include operators like the conditional (ternary) operator and the null-coalescing operators.

  • Conditional (ternary) Operator (?:): Evaluates a Boolean expression and returns one of two values depending on whether the expression is true or false.

Syntax

condition ? value_if_true : value_if_false;

Example

int a = 10;
string result = (a > 5) ? "Greater than 5" : "Less than or equal to 5";
Console.WriteLine($"Result: {result}");

Output:

Result: Greater than 5
  • Null-coalescing Operator (??): Returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.

Example

string str = null;
string result = str ?? "Default value";
Console.WriteLine($"Result: {result}");

Output:

Result: Default value
  • Null-coalescing Assignment Operator (??=): Assigns the right-hand operand to the left-hand operand only if the left-hand operand is null.

Example

string str = null;
str ??= "Assigned value";
Console.WriteLine($"str: {str}");

Output:

str: Assigned value

Conclusion

Operators are essential tools for performing various operations in C#. Understanding and using arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators allows you to write powerful and efficient code. By mastering these operators,you can manipulate data, make decisions, and control the flow of your programs effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top