Lesson 3 – Operators in C#

Operators perform different types of operations on constants and variables in C#. For instance, if you want to add two numbers, you will have to use the addition operator. Similarly, to perform logical AND operation between boolean data types, you will need the logical AND operators. In this lesson, you will see what are the different types of operators that C# supports.

Operators in C# can be broadly divided into four categories as shown below:

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

Arithmetic Operators

The arithmetic operator is used to perform arithmetic operations such as addition, subtraction, multiplication, increment, etc. The following table summarizes some of the most commonly used arithmetic operators in C#.

.tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg .tg-cly1{text-align:left;vertical-align:middle} .tg .tg-yla0{font-weight:bold;text-align:left;vertical-align:middle} .tg .tg-0lax{text-align:left;vertical-align:top}
Operator Name Functionality Example
+ Addition Performs addition on two numbers a + b
Subtraction Performs subtraction on two numbers a – b
* Multiplication Performs multiplication on two numbers a * b
/ Division Divides one value from another a / b
% Modulus Returns the division remainder a % b
++ Increment Increases the value of a variable by 1 ++a
Decrement Decreases the value of a variable by 1 –a

Let’s see a simple example of some of the arithmetic operators:


namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 25;
            int y = 5;

            int result = x + y;
            Console.WriteLine("Addition: "+result);

            result = x - y;
            Console.WriteLine("Subtraction: " + result);

            result = x * y;
            Console.WriteLine("Multiplication: " + result);

            result = x / y;
            Console.WriteLine("Division: " + result);

            result = x % y;
            Console.WriteLine("Modulus: " + result);

            result = ++x;
            Console.WriteLine("Increment: " + result);

        }
    }
}

Here is the output of the script above:

Addition: 30
Subtraction: 20
Multiplication: 125
Division: 5
Modulus: 0
Increment: 26

Assignment Operators

Assignment operators in C# are used to assign the value of a variable or a constant to another variable. The following table contains the assignment operators in C#:

.tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg .tg-cly1{text-align:left;vertical-align:middle} .tg .tg-yla0{font-weight:bold;text-align:left;vertical-align:middle}
Operator Name Example Is Equal to
= Assigns a = 10 a = 10
+= Add and Assign a += 5 a = a + 5
-= Subtract and Assign a -= 5 a = a – 5
*= Multiply and Assign a *= 5 a = a * 5
/= Divide and Assign a /= 5 a = a / 5
%= Take modulus and assign a %= 5 a = a % 5

Let’s see a simple example of assignment operators:

{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 25;
            int y = 5;

            int result = 10;
            Console.WriteLine("Assign: "+result);

            result += 10;
            Console.WriteLine("Add and Assign: " + result);

            result -= 10;
            Console.WriteLine("Subtract and Assign: " + result);

            result *= 10;
            Console.WriteLine("Multiply and Assign: " + result);

            result /= 10;
            Console.WriteLine("Divide and Assign: " + result);

            result %= 10;
            Console.WriteLine("Modulus and Assign: " + result);
        }
    }
}

Here is the output of the above script:

Assign: 10
Add and Assign: 20
Subtract and Assign: 10
Multiply and Assign: 100
Divide and Assign: 10
Modulus and Assign: 0

Comparison Operators

Comparison operators are used for comparing the value of one variable or constant to another. For instance, if you want to find which of the two variables is greater, you can use comparison operators. Here is a table that contains all the comparison operators in C#:

.tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg .tg-cly1{text-align:left;vertical-align:middle}
Operator Name Example
== Equal to a == b
!= Not equal a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

Let’s now see a simple example of Operators in C#:

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 25;
            int y = 5;

            bool result = x > y;
            Console.WriteLine("Greater than: "+result);

            result = x < y;
            Console.WriteLine("Less than: " + result);

            result = x != y;
            Console.WriteLine("Not equal to: " + result);

        }
    }
}

Here is the output of the above script:

Greater than: True
Less than: False
Not equal to: True

Logical Operators

The following table contains the logical operators supported by C#.

.tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;} .tg .tg-lboi{border-color:inherit;text-align:left;vertical-align:middle} .tg .tg-g7sd{font-weight:bold;border-color:inherit;text-align:left;vertical-align:middle}
Operator Name Description Example
&& AND operator Returns true if both operands are true a < 5 && a < 20
|| OR operator Returns true if one of the operands is true a < 5 ||  a < 20
! NOT operator Reverse the result !(x < 10)

Let’s see a working example of Logical operators:

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 25;
            int y = 5;

            bool result = x > y && x == y;
            Console.WriteLine("Greater than and equal to: "+result);

            result = !(x < y);
            Console.WriteLine("Not less than: " + result);


        }
    }
}

Here is the output of the above script:

Greater than and equal to: False
Not less than: True

What’s Next?

In this lesson, you studied C# operators. In the next lesson, we are going to start our discussion about Arrays in C#.