Skip to main content

Operators

Introduction to Operators in C#

Operators in C# are symbols that are used to perform operations on variables and values. The most common operators are used to perform arithmetic operations, comparison operations or logical operations.

Types of Operators in C#

There are several types of operators in C#, each with a specific purpose:

  1. Arithmetic Operators: These are used to perform mathematical operations such as addition, subtraction, multiplication, division, modulus, etc.

  2. Relational Operators: These are used to compare two values. They include equal to, not equal to, less than, greater than, less than or equal to, and greater than or equal to.

  3. Logical Operators: These are used to combine two or more conditions. They include AND, OR, and NOT.

  4. Assignment Operators: These are used to assign values to variables.

  5. Unary Operators: These are used to perform operations on a single operand. They include increment, decrement, and logical complement.

  6. Bitwise Operators: These are used to perform bit-level operations.

  7. Ternary Operator: It is a special operator that takes three operands and therefore, it's also known as a conditional operator.

Let's go through each type of operator in more detail:

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations. The basic arithmetic operators in C# are:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus - remainder of division)

Example:

int a = 10;
int b = 2;
Console.WriteLine(a + b); // Output: 12
Console.WriteLine(a - b); // Output: 8
Console.WriteLine(a * b); // Output: 20
Console.WriteLine(a / b); // Output: 5
Console.WriteLine(a % b); // Output: 0

Relational Operators

Relational operators are used to compare two values. Here are the relational operators in C#:

  • == (Equal to)
  • != (Not equal to)
  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)

Example:

int a = 10;
int b = 20;
Console.WriteLine(a == b); // Output: False
Console.WriteLine(a != b); // Output: True
Console.WriteLine(a < b); // Output: True
Console.WriteLine(a > b); // Output: False
Console.WriteLine(a <= b); // Output: True
Console.WriteLine(a >= b); // Output: False

Logical Operators

Logical operators are used to combine two or more conditions. The logical operators in C# are:

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

Example:

bool a = true;
bool b = false;
Console.WriteLine(a && b); // Output: False
Console.WriteLine(a || b); // Output: True
Console.WriteLine(!a); // Output: False

Assignment Operators

Assignment operators are used to assign values to variables. Here are the assignment operators in C#:

  • = (Assign)
  • += (Add and Assign)
  • -= (Subtract and Assign)
  • *= (Multiply and Assign)
  • /= (Divide and Assign)
  • %= (Modulus and Assign)

Example:

int a = 10;
a += 2; // equivalent to a = a + 2;
Console.WriteLine(a); // Output: 12

Unary Operators

Unary operators are used to perform operations on a single operand. The unary operators in C# are:

  • ++ (Increment)
  • -- (Decrement)

Example:

int a = 10;
a++; // equivalent to a = a + 1;
Console.WriteLine(a); // Output: 11

Bitwise Operators

Bitwise operators are used to perform bit-level operations. The bitwise operators in C# are:

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise complement)
  • << (Left shift)
  • >> (Right shift)

Example:

int a = 10; // Binary: 1010
int b = 6; // Binary: 0110
Console.WriteLine(a & b); // Output: 2 (Binary: 0010)

Ternary Operator

The ternary operator is a special operator in C# that takes three operands. It is a shorthand for the if-else condition. The syntax is condition ? expression_if_true : expression_if_false.

Example:

int a = 10;
int b = 20;
int max = a > b ? a : b;
Console.WriteLine(max); // Output: 20

Conclusion

Operators are fundamental to any programming language, and C# is no exception. They allow us to perform operations on values and make decisions based on conditions. Understanding how to use these operators is crucial to becoming a successful C# programmer.