C# Operators

Operators and it's expressions in C#.

C & C# their operators are same? While C and C# (C Sharp) share some common operators, there are also differences in their operator sets due to the distinct features and paradigms of each language. C# introduces additional operators and features that are not present in standard C, particularly because C# is designed as an object-oriented language with modern programming paradigms.

Operators and Expressions

C# provides a variety of operators that perform operations on variables and values. Here's a list of some common operators in C#:

Arithmetic Operators:

Arithmetic operators perform basic mathematical operations.

Operators Meanings
+ Addition: Adds two operands.
- Subtraction: Subtracts the right operand from the left operand.
* Multiplication: Multiplies two operands.
/ Division: Divides the left operand by the right operand.
% Modulus: Returns the remainder of the division of the left operand by the right operand.

Example:

int a = 10;
int b = 3;
int additionResult = a + b;  // 13
int subtractionResult = a - b;  // 7
int multiplicationResult = a * b;  // 30
int divisionResult = a / b;  // 3
int modulusResult = a % b;  // 1

Relational Operators:

Relational operators are used to compare values and determine the relationship between them.

Operators Meanings
== Equality: Checks if the values of two operands are equal.
!= Inequality: Checks if the values of two operands are not equal.
< Less than: Checks if the value of the left operand is less than the value of the right operand.
> Greater than: Checks if the value of the left operand is greater than the value of the right operand.
<= Less than or equal to: Checks if the value of the left operand is less than or equal to the value of the right operand.
>= Greater than or equal to: Checks if the value of the left operand is greater than or equal to the value of the right operand.

Example:

int x = 5;
int y = 10;
bool isEqual = (x == y);  // Equality
bool isNotEqual = (x != y);  // Inequality
bool isLessThan = (x < y);  // Less than
bool isGreaterThan = (x > y);  // Greater than
bool isLessThanOrEqual = (x <= y);  // Less than or equal to
bool isGreaterThanOrEqual = (x >= y);  // Greater than or equal to

Logical Operators:

Logical operators perform logical operations on boolean values.

Operators Meanings
&& Logical AND: Returns true if both operands are true.
|| Logical OR: Returns true if at least one of the operands is true.
! Logical NOT: Returns true if the operand is false, and vice versa.

Example:

bool isTrue = true;
bool isFalse = false;
bool logicalAnd = (isTrue && isFalse);  // Logical AND
bool logicalOr = (isTrue || isFalse);  // Logical OR
bool logicalNot = !isTrue;  // Logical NOT

Assignment Operators:

Logical operators perform logical operations on boolean values.

Operators Meanings
= Assignment: Assigns the value on the right to the variable on the left.
+= Addition Assignment: Adds the right operand to the left operand and assigns the result to the left operand.
-= Subtraction Assignment: Subtracts the right operand from the left operand and assigns the result to the left operand.
*= Multiplication Assignment: Multiplies the left operand by the right operand and assigns the result to the left operand.
/= Division Assignment: Divides the left operand by the right operand and assigns the result to the left operand.
%= Modulus Assignment: Performs modulus on the left operand with the right operand and assigns the result to the left operand.

Example:

int a = 5;
int b = 10;
a += b;  // Addition assignment (equivalent to a = a + b)
a -= b;  // Subtraction assignment (equivalent to a = a - b)
a *= b;  // Multiplication assignment (equivalent to a = a * b)
a /= b;  // Division assignment (equivalent to a = a / b)
a %= b;  // Modulus assignment (equivalent to a = a % b)

Increment & Decrement Operators:

Increment and decrement operators are used to increase or decrease the value of a variable by 1.

Operators Meanings
++ Increment: Increases the value of the operand by 1.
-- Decrement: Decreases the value of the operand by 1.

Example:

int count = 5;
count++;  // Increment by 1 (equivalent to count = count + 1)
count--;  // Decrement by 1 (equivalent to count = count - 1)

Conditional Operator:

The conditional (ternary) operator is a shorthand way to express an if-else statement in a single line.

Operator Meanings
? : Conditional: Evaluates a boolean expression and returns one of two values based on the result.

Example:

int value = (x > y) ? x : y;
/* If x is greater than y, value is assigned x;
 otherwise, it's assigned y. */

Bitwise Operators:

Bitwise operators are used to perform bitwise operations on integer-type variables.

Operator Meanings
& Bitwise AND: Performs a bitwise AND operation.
| Bitwise OR: Performs a bitwise OR operation.
^ Bitwise XOR: Performs a bitwise exclusive OR (XOR) operation.
~ Bitwise NOT: Performs a bitwise NOT operation (flips the bits).
<< Left Shift: Shifts the bits of a number to the left by a specified number of positions.
>> Right Shift: Shifts the bits of a number to the right by a specified number of positions.

Example:

 // Bitwise AND
int resultAnd = a & b; // Binary: 0101 & 0011 = 0001
        Console.WriteLine($"Bitwise AND: {resultAnd}"); // Output: 1

// Bitwise OR
                                int resultOr = a | b; // Binary: 0101 | 0011 = 0111
        Console.WriteLine($"Bitwise OR: {resultOr}"); // Output: 7

// Bitwise XOR
        int resultXor = a ^ b; // Binary: 0101 ^ 0011 = 0110
        Console.WriteLine($"Bitwise XOR: {resultXor}"); // Output: 6

// Bitwise NOT
        int resultNotA = ~a; // Binary: ~0101 = 1010 (in two's complement form)
        Console.WriteLine($"Bitwise NOT (a): {resultNotA}"); // Output: -6


 // Left Shift
        int resultLeftShift = a << 2; // Binary: 0101 << 2 = 010100 
        Console.WriteLine($"Left Shift: {resultLeftShift}"); // Output: 20

// Right Shift
        int resultRightShift = a >> 1; // Binary: 0101 >> 1 = 0010
        Console.WriteLine($"Right Shift: {resultRightShift}"); // Output: 2

These operators play a crucial role in C# programming and are used for various tasks such as mathematical calculations, comparisons, logical operations, and more.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.