C# Operators

C# is a powerful programming language that allows developers to manipulate data using a variety of operators. Operators are special symbols or keywords that perform operations on one or more operands. In this article, we will discuss various types of operators in C# with examples.

Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic operations like addition, subtraction, multiplication, and division. The following table lists the arithmetic operators in C#:

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (Remainder)

Here are some examples of using arithmetic operators in C#:

1int num1 = 10, num2 = 5; 2int sum = num1 + num2; 3int difference = num1 - num2; 4int product = num1 * num2; 5int quotient = num1 / num2; 6int remainder = num1 % num2;

In the above example, we have used all the arithmetic operators to perform basic mathematical operations.

Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (true or false) based on the comparison. The following table lists the comparison operators in C#:

OperatorDescription
==Equal to
!=Not equal to
1 | Greater than

< | Less than

= | Greater than or equal to <= | Less than or equal to

Here are some examples of using comparison operators in C#:

1int num1 = 10, num2 = 5; 2bool isEqual = num1 == num2; // false 3bool isNotEqual = num1 != num2; // true 4bool isGreaterThan = num1 > num2; // true 5bool isLessThan = num1 < num2; // false 6bool isGreaterThanOrEqual = num1 >= num2; // true 7bool isLessThanOrEqual = num1 <= num2; // false

In the above example, we have used all the comparison operators to compare two numbers and get a Boolean value.

Logical Operators

Logical operators are used to perform logical operations on Boolean values. The following table lists the logical operators in C#:

OperatorDescription
&&Logical AND
||Logical OR
!Logical NOT

Here are some examples of using logical operators in C#:

1bool a = true, b = false, c = true; 2bool result1 = a && b; // false 3bool result2 = a || b; // true 4bool result3 = !a; // false 5bool result4 = b || (a && c); // true

In the above example, we have used all the logical operators to perform logical operations on Boolean values.

Assignment Operators

Assignment operators are used to assign values to variables. The following table lists the assignment operators in C#:

OperatorDescription
=Assigns the value on the right to the variable on the left
+=Adds the value on the right to the variable on the left
-=Subtracts the value on the right from the variable on the left
*=Multiplies the value on the right with the variable on the left
/=Divides the variable on the left by the value on the right
%=Assigns the remainder of the variable on the left divided by the value on the right to the variable on the left
1int num1 = 10, num2 = 5; 2num1 += num2; // num1 = 15 3num1 -= num2; // num1 = 10 4num1 *= num2; // num1 = 50 5num1 /= num2; // num1 = 10 6num1 %= num2; // num1 = 0

In the above example, we have used all the assignment operators to assign values to a variable.

Bitwise Operators

Bitwise operators are used to perform operations on the binary representation of the operands. The following table lists the bitwise operators in C#:

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Bitwise left shift
1 | Bitwise right shift

Here are some examples of using bitwise operators in C#:

1int num1 = 10, num2 = 5; 2int result1 = num1 & num2; // 0 3int result2 = num1 \| num2; // 15 4int result3 = num1 ^ num2; // 15 5int result4 = ~num1; // -11 6int result5 = num1 << 1; // 20 7int result6 = num1 >> 1; // 5

In the above example, we have used all the bitwise operators to perform operations on the binary representation of the operands.