Operators in C# with Examples

Operators are useful not only in C# programming but in any programming language. Because without operators, we cannot perform operations. Therefore, operators are used to perform operations in a program. For example:

int sum = a + b;

Using the above C# statement, whatever the values of the variables a and b are, they will be added together using the + operator and set to the sum variable using the = operator.

In this post, we will discuss these four types of operators.

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators

C# Arithmetic operators

Here is the list of all arithmetic operators available in C#, along with their brief descriptions.

Operator Name and Description
+ The addition operator is used to perform addition.
- Subtraction operator, used to perform subtraction
* Multiplication operator, used to perform multiplication
/ A division operator is used to perform division.
% The modulus operator, used to get the remainder while performing the division.
++ The increment operator is used to increment the value by 1.
-- The decrement operator is used to decrement the value by 1.

For example:

int a = 13, b = 5;

Console.WriteLine(a + b);
Console.WriteLine(a - b);
Console.WriteLine(a * b);
Console.WriteLine(a / b);
Console.WriteLine(a % b);

a++;
Console.WriteLine(a);
b--;
Console.WriteLine(b);

The output produced by this C# example should exactly be:

18
8
65
2
3
14
4

C# Assignment operators

Here is the list of all assignment operators available in C#, along with their brief descriptions.

Operator Name and description
= simple assignment operator, used to assign the value of the right operand to the left operand.
+= The add then assign operator is used to perform the summation of left and right operands and then assign the result to the left operand.
-= The operator "subtract then assign" is used to perform the subtraction of the right operand from the left and then assign the result to the left operand.
*= With the multiply-then-assign operator, the left and right operands are multiplied, and the result is given to the left operand.
/= Divide then assign is an operator used to perform division from left to right with the right operand and then assign the result to the left operand.
%= Take modulus, then assign operator. similar to the /= operator, but instead of performing division, it performs modulus. And instead of assigning a division result, it assigns the remainder.
<<= Left shift and assign operator used to take the left shift then assign the result.
>>= Right shift and assign operator used to take the right shift and then assign the result.
&= The bitwise and assign operators are used to find the bitwise result and then assign the result.
^= Bitwise exclusive OR and the assign operator are used to find the bitwise exclusive OR and then assign the result.
|= Bitwise inclusive OR and the assign operator are used to find the bitwise inclusive OR and then assign the result.

For example:

int x, y;

x = 10;
Console.WriteLine(x);

x += 2;
Console.WriteLine(x);

x -= 2;
Console.WriteLine(x);

x *= 3;
Console.WriteLine(x);

x /= 3;
Console.WriteLine(x);

x %= 4;
Console.WriteLine(x);

x &= 3;
Console.WriteLine(x);

x |= 3;
Console.WriteLine(x);

y = 10;
y >>= 3;
Console.WriteLine(y);

y <<= 3;
Console.WriteLine(y);

The output of this C# example should exactly be:

10
12
10
30
10
2
2
3
1
8

C# Comparison operators

Here is the list of all comparison operators available in C#, along with their brief descriptions.

Operator Name and description
== Equal to operator, used to check whether two values are equal or not
!= Not equal operator, used to check if two values are not equal
> Greater than operator, used to check if first value is greater than the second
< Less than operator, used to check if first value is less than the second
>= Greater than or equal to, used to check if first value is greater than or equal to the second
<= Less than or equal to operator, used to check if first value is less than or equal to the second

For example:

int x = 10, y = 20, z = 10;

Console.WriteLine(x == y);
Console.WriteLine(x == z);
Console.WriteLine(x != y);
Console.WriteLine(x > y);
Console.WriteLine(x < y);
Console.WriteLine(x >= y);
Console.WriteLine(x <= y);

The output of this C# example should exactly be:

False
True
True
False
True
False
True

C# Logical operators

Here is the list of all logical operators available in C#, along with their brief descriptions.

Operator Name and description
&& Logical AND operator. Returns True if both the specified statements are true. Otherwise returns false
|| Logical OR operator. Returns true if one of the specified statements is true. Otherwise returns false
! Logical NOT operator. This operator is used to reverse the result

For example:

int x = 10, y = 20, z = 30;

Console.WriteLine(x < y && y < z);
Console.WriteLine(x < y && x > z);

Console.WriteLine(x < y || y < z);
Console.WriteLine(x < y || x > z);

Console.WriteLine(!true);
Console.WriteLine(!(x < y || x > z));

The output should exactly be:

True
False
True
True
False
False

C# Online Test


« Previous Tutorial Next Tutorial »