C# Operators


In C#, operators are special symbols that perform operations on one or more operands (variables or values). Operators can be categorized based on the type of operation they perform. Here’s a detailed explanation of the different types of operators in C#:

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example:

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

2. Relational Operators

Relational operators are used to compare two operands and return a boolean value (true or false).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

int a = 5; int b = 10; bool isEqual = (a == b); // isEqual = false bool isGreater = (a > b); // isGreater = false bool isLessOrEqual = (a <= b); // isLessOrEqual = true

3. Logical Operators

Logical operators are used to perform logical operations, often with boolean values.

OperatorDescriptionExample
&&Logical AND(a > b) && (c < d)
``
!Logical NOT!(a > b)

Example:

bool condition1 = true; bool condition2 = false; bool resultAnd = condition1 && condition2; // resultAnd = false bool resultOr = condition1 || condition2; // resultOr = true bool resultNot = !condition1; // resultNot = false

4. Bitwise Operators

Bitwise operators perform operations on individual bits of integral types.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << 1
>>Right shifta >> 1

Example:

int a = 5; // 0101 in binary int b = 3; // 0011 in binary int resultAnd = a & b; // resultAnd = 1 (0001) int resultOr = a | b; // resultOr = 7 (0111) int resultXor = a ^ b; // resultXor = 6 (0110) int resultNot = ~a; // resultNot = -6 (inverts bits) int leftShift = a << 1; // leftShift = 10 (1010) int rightShift = a >> 1; // rightShift = 2 (0010)

5. Assignment Operators

Assignment operators are used to assign values to variables. They can also perform operations while assigning values.

OperatorDescriptionExample
=Assignmenta = b
+=Add and assigna += b (equivalent to a = a + b)
-=Subtract and assigna -= b
*=Multiply and assigna *= b
/=Divide and assigna /= b
%=Modulus and assigna %= b
&=Bitwise AND and assigna &= b
`=`Bitwise OR and assign
^=Bitwise XOR and assigna ^= b
<<=Left shift and assigna <<= b
>>=Right shift and assigna >>= b

Example:

int a = 5; a += 3; // a = 8 a -= 2; // a = 6

6. Unary Operators

Unary operators operate on a single operand.

OperatorDescriptionExample
++Increment++a or a++
--Decrement--a or a--
+Unary plus (no effect)+a
-Unary negation-a
!Logical negation!a

Example:

int a = 5; a++; // a = 6 (post-increment) ++a; // a = 7 (pre-increment) a--; // a = 6 (post-decrement) --a; // a = 5 (pre-decrement)

7. Ternary Operator

The ternary operator is a shorthand for the if-else statement. It has the following syntax:

condition ? expressionIfTrue : expressionIfFalse;

Example:

int a = 10; int b = 20; int max = (a > b) ? a : b; // max = 20

8. Null-Coalescing Operator

The null-coalescing operator (??) is used to define a default value when a nullable type or reference type is null.

Example:

string name = null; string defaultName = name ?? "Default Name"; // defaultName = "Default Name"

9. Type Comparison Operator

The is operator is used to check if an object is compatible with a given type. The as operator attempts to cast an object to a specified type, returning null if the cast fails.

Example:

object obj = "Hello, World!"; if (obj is string str) // Check type and cast { Console.WriteLine(str); // Output: Hello, World! } string anotherStr = obj as string; // Attempt to cast if (anotherStr != null) { Console.WriteLine(anotherStr); // Output: Hello, World! }

Summary

Operators in C# allow developers to perform a wide range of operations on variables and values. Here’s a recap of the main types of operators:

  • Arithmetic Operators: For mathematical calculations.
  • Relational Operators: For comparisons.
  • Logical Operators: For boolean logic.
  • Bitwise Operators: For operations on bits.
  • Assignment Operators: For assigning values.
  • Unary Operators: For operations on a single operand.
  • Ternary Operator: For conditional assignments.
  • Null-Coalescing Operator: For handling null values.
  • Type Comparison Operator: For checking types and casting.

Understanding these operators is essential for effective programming in C#, enabling you to write efficient and functional code.