In this article I am going to discuss about the C# Operators in detail but before that if you had not read my previous article of C# Type Casting then must read it. In every programming language operators play an important role for writing programs. Operators are considered as base of any programming language. In C# Operators plays a crucial role to make complex programs. There are different-2 types of operators are there in C# Programming language.

Introduction to Operators

Operators in computer programming are symbols or keywords that dictate specific actions or operations to be performed on operands. Operands can be variables, constants, or expressions, and the combination of operators and operands results in a new value. Operators play a fundamental role in expressing computations, conditions, and transformations within the syntax of programming languages, enabling the creation of complex algorithms and logical structures.

Types of C# Operators

There are different-different types of C# Operators are there for different usage. Every type of C# Operator has an own importance and usage. The following are the operators in C# Programming language:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Unary Operators
  • Ternary Operator or Conditional Operator

Arithmetic Operators

In C#, arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus on operands. Operands are the numeric values on which you want to perform operations. All the artithmetic operators are discussed below:

Addition Operator (+)

The addition operator (+) is used to add two operands(numbers). This operator is also known as binary operator because it takes two operands and returns the result. For example:

int firstNumber = 5;
int secondNumber = 10;
int result=firstNumber + secondNumber;//it will add the firstNumber and secondNumber operand values 10 + 5 and return their sum.

Subtraction Operator (-)

The subtraction operator (-) is used to subtract two operands. This operator is also known as binary operator because it takes two operands and returns the result. For example:

int firstNumber = 15;
int secondNumber = 10;
int result=firstNumber - secondNumber;

Multiplication Operator (*)

The multiplication operator (*) is used to mulitply two operands. This operator is also known as binary operator because it takes two operands and returns the result. For example:

int firstNumber = 15;
int secondNumber = 10;
int result=firstNumber * secondNumber;

Divison Operator (/)

The divison operator (*) is used to divide two operands. This operator is also known as binary operator because it takes two operands and returns the result. For example:

int firstNumber = 15;
int secondNumber = 5;
int result=firstNumber / secondNumber;

Modulus Operator (%)

The divison operator (%) is used to return the remainder when we divide two numbers. This operator is also known as binary operator because it takes two operands and returns the result. For example:

int firstNumber = 17;
int secondNumber = 5;
int result=firstNumber / secondNumber; // result=2

C# Program to Explain Arithmetic Operators

using System;
namespace ArithmeticOperatorsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int Result;
            int Num1 = 20, Num2 = 10;

            // Addition Operation
            Result = (Num1 + Num2);
            Console.WriteLine("Addition Operator: "+ Result );

            // Subtraction Operation
            Result = (Num1 - Num2);
            Console.WriteLine("Subtraction Operator: "+ Result );

            // Multiplication Operation
            Result = (Num1 * Num2);
            Console.WriteLine("Multiplication Operator: "+ Result );

            // Division Operation
            Result = (Num1 / Num2);
            Console.WriteLine("Divison Operator: "+ Result );

            // Modulus Operation
            Result = (Num1 % Num2);
            Console.WriteLine("Modulus Operator: "+ Result );
            Console.ReadKey();
        }
    }
}
Output:
Addition Operator: 30
Subtraction Operator: 10
Multiplication Operator: 200
Divison Operator: 2
Modulus Operator: 0

Relational Operators

In C#, Relational Operators are also known as Comparison Operators. It determines the relationship between two operands and returns the Boolean results. All relational operators are discussed below:

Greater Than (>) Operator

This operator returns true if the left hand side of operator is greater than right hand side operator. This operator is also known as binary operator because it takes two operands to produce the result.

Greater Than or Equal to (>=)

This operator returns true if the left hand side of operator is greater than or equal to right hand side operator. This operator is also known as binary operator because it takes two operands to produce the result.

Less Than (<) Operator

This operator returns true if the left hand side of operator is less than right hand side of operator. This operator is also known as binary operator because it takes two operands to produce the result.

Less Than or Equal to (<=) Operator

This operator returns true if the left hand side of operator is less than or equal to right hand side operator. This operator is also known as binary operator because it takes two operands to produce the result.

Equal to (==) Operator

This operator returns true if the left hand side of operator is equal to right hand side operator. This operator is also known as binary operator because it takes two operands to produce the result.

Not Equal to (!=) Operator

This operator returns true if the left hand side of operator is not equal to right hand side operator. This operator is also known as binary operator because it takes two operands to produce the result.

C# Program to Explain Relational Operators

using System;
namespace RelationalOperatorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            bool Result;
            int Num1 = 5, Num2 = 10;

            // Equal to Operator
            Result = (Num1 == Num2);
            Console.WriteLine("Equal (=) to Operator: " + Result);

            // Greater than Operator
            Result = (Num1 > Num2);
            Console.WriteLine("Greater (&lt;) than Operator: " + Result);

            // Less than Operator
            Result = (Num1 < Num2);
            Console.WriteLine("Less than (&gt;) Operator: " + Result);

            // Greater than Equal to Operator
            Result = (Num1 >= Num2);
            Console.WriteLine("Greater than or Equal to (&gt;=) Operator: " + Result);

            // Less than Equal to Operator
            Result = (Num1 <= Num2);
            Console.WriteLine("Lesser than or Equal to (&lt;=) Operator: " + Result);

            // Not Equal To Operator
            Result = (Num1 != Num2);
            Console.WriteLine("Not Equal to (!=) Operator: " + Result);

            Console.ReadKey();
        }
    }
}
Output:
Equal (=) to Operator: False
Greater (<) than Operator: False
Less than (>) Operator: True
Greater than or Equal to (>=) Operator: False
Lesser than or Equal to (<=) Operator: True
Not Equal to (!=) Operator: True

Logical Operators

In C#, logical operators are symbols used to perform Boolean operations on Boolean expressions. Basically this type of operators are used in conditional statements and loops for evaluating particular condition. This type of operators are also called binary operators as it takes two operands and return the boolean result. All Logical Operators are discussed below:

Logical OR (||) Operator

This Operator returns true if any of the condition is true. It is denoted by (||). In given code there is one if condition where I check for two conditions. If any of the conditions or both conditions are true then if statement executes otherwise else statement executes.

int firstNumber=5;
int secondNumber=10;
if(firstNumber==5 || firstNumber >secondNumber){
    Console.WriteLine("First Number is 5"); 
}
else{
  Console.WriteLine("First Number is greater than second Number");
}

Logical AND (&&) Operator

This Operator returns true if all of the condition is true. It is denoted by (||). In given code there is one if condition where I check for two conditions. If both conditions are true then if statement executes otherwise else statement executes.

int firstNumber=5;
int secondNumber=10;
if(firstNumber==5 && secondNumber > firstNumber){
    Console.WriteLine("First Number is 5"); 
}
else{
  Console.WriteLine("First Number is greater than second Number");
}

Logical Not (!) Operator

This operator returns true if the condition result is false. This is called unary operator.

C# Program to Explain Logical Operators

using System;
namespace LogicalOperatorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            bool x = true, y = false, z;

            //Logical AND operator
            z = x && y;
            Console.WriteLine("Logical AND Operator (&&) : " + z);

            //Logical OR operator
            z = x || y;
            Console.WriteLine("Logical OR Operator (||) : " + z);

            //Logical NOT operator
            z = !x;
            Console.WriteLine("Logical NOT Operator (!) : " + z);

            Console.ReadKey();
        }
    }
}
Output:
Logical AND Operator (&&) : False
Logical OR Operator (||) : True
Logical NOT Operator (!) : False

Bitwise Operators

The Bitwise Operators in C# perform bit-by-bit processing. The bitwise operator returns 1 for true and 0 for false. To use bitwise Operators in your program then first you have to find the binary number of corresponding number then you have to perform bitwise operation on the binary numbers. Bitwise Operators are very fast as compared to other operators. All the bitwise operators in C# are discussed below:

Bitwise OR ( | ) Operator

In C# operators this bitwise operator performs the bitwise OR operation on the corresponding bits of the two operands involved in the operation. If either of the bits is 1, it gives 1. If not, it gives 0. For example:

int firstNumber=12;           //binary value = 00001100
int secondNumber=25;          //binary value = 00011001
int result = firstNumber| secondNumber;//      00011101 its decimal value is 29
//Hence result is 29.

Bitwise AND (&) Operator

This bitwise operator performs the bitwise AND operation on the corresponding bits of the two operands involved in the operation. If both of the bits is 1 , it gives 1. If not, it gives 0. For example:

int firstNumber=12;           //binary value = 00001100
int secondNumber=25;          //binary value = 00011001
int result = firstNumber| secondNumber;//      00001000 its decimal value is 8
//Hence result is 8.

Bitwise XOR (^) Operator

This operator performs a bitwise XOR operation on the corresponding bits of two operands. If the both bits are different, it gives 1. If the both bits are the same, it gives 0. For example:

int firstNumber=12;           //binary value = 00001100
int secondNumber=25;          //binary value = 00011001
int result = firstNumber| secondNumber;//      00010101 its decimal value is 21
//Hence result is 21.

Bitwise Ones Complement (~) Operator

This bitwise operator is used to find the ones complement of number. You can find the ones complement of any number by reversing its bits means change 0 to 1 and 1 to 0. This operator is also called unary operator because it only need one operand to perform operation. For example:

int firstNumber=12;    //binary value=00001100
int result =~firstNumber //           11110011 its decimal value is 243
//hence result is 243

Bitwise Left Shift (<< ) Operator

This operator is used to shift the bits. The left operands value is moved left by the number of bits specified by the right operand. For example:

int firstNumber = 60;  //binary value=00111100
int result = firstNumber << 2; //     11110000 its decimal value is 240
//hence result is 240

Bitwise Right Shift (>>) Operator

This operator is used to shift the bits. The left operands value is moved right by the number of bits specified by the right operand. For example:

int firstNumber = 60;  //binary value=00111100
int result = firstNumber >> 2; //     00001111 its decimal value is 15
//hence result is 15

C# Program to Explain Bitwise Operators

using System;
namespace BitwiseOperatorDemo {
   class Program {
   
      static void Main(string[] args) {
         int a = 60;            /* 60 = 0011 1100 */ 
         int b = 13;            /* 13 = 0000 1101 */
         int c = 0; 
         
         c = a & b;             
         Console.WriteLine("Line 1 - Value of c is {0}", c );
         
         c = a | b;             
         Console.WriteLine("Line 2 - Value of c is {0}", c);
         
         c = a ^ b;             
         Console.WriteLine("Line 3 - Value of c is {0}", c);
         
         c = ~a;             
         Console.WriteLine("Line 4 - Value of c is {0}", c);
         
         c = a << 2;      
         Console.WriteLine("Line 5 - Value of c is {0}", c);
         
         c = a >> 2;      
         Console.WriteLine("Line 6 - Value of c is {0}", c);
         Console.ReadLine();
      }
   }
}
Output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

Assignment Operators

In C# Operators, assignment operators are one of the important Operators that is used to assign a value to a variable. The left-hand side operand of the assignment operator is a variable and the right-hand side operand of the assignment operator can be a value or an expression that must return some value and that value is going to assign to the left-hand side variable. Remember the right hand side expression is of same data type of variable in which you are assigning the value. All the types of assignment operators are discussed below:

Simple Assignment ( = ) Operator

This operator is used to assign the right hand side value to left hand side variable. For example int a = 6;

Add Assignment ( += ) Operator

This operator is the combination of + and = operators. It is used to add left side operand value with right side operand value and then assign the result to the left-hand side variable. For example:

a=a+b; //It is same as a+=b

Subtract Assignment ( -= ) Operator

This operator is the combination of – and = operators. It is used to subtract left side operand value with right side operand value and then assign the result to the left-hand side variable. For example:

a=a-b; //It is same as a-=b

Multiply Assignment ( *= ) Operator

This operator is the combination of * and = operators. It is used to multiply left side operand value with right side operand value and then assign the result to the left-hand side variable. For example:

a=a*b; //It is same as a*=b

Divison Assignment ( /= ) Operator

This operator is the combination of / and = operators. It is used to divide left side operand value with right side operand value and then assign the result to the left-hand side variable. For example:

a=a/b; //It is same as a/=b

Modulus Assignment ( %= ) Operator

This operator is the combination of % and = operators. It is used to divide left side operand value with right side operand value and then assign the result to the left-hand side variable. For example:

a=a%b; //It is same as a%=b

C# Program to Explain Assignment Operators

using System;
namespace AssignmentOperatorsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize variable x using Simple Assignment Operator "="
            int x = 15;

            x += 10;
            Console.WriteLine("Add Assignment Operator: {0}",x);

            x -= 5; 
            Console.WriteLine("Subtract Assignment Operator: {0}",x);

            x *= 5;
            Console.WriteLine("Multiply Assignment Operator: {0}",x);

            x /= 5;
            Console.WriteLine("Division Assignment Operator: {0}",x);

            x %= 5;
            Console.WriteLine("Modulus Assignment Operator: {0}",x);

            Console.ReadKey();
        }
    }
}
Output:
Add Assignment Operator: 25
Subtract Assignment Operator: 20
Multiply Assignment Operator: 100
Division Assignment Operator: 20
Modulus Assignment Operator: 0

Unary Operators

The Operator which only need one operand to perform any operation is called Unary Operators. All the main unary operators are discussed below:

Increment ( ++ ) Operator

The increment operator is again classified into two categories.

  • Post-Increment Operator
  • Pre-Increment Operator
Post-Increment Operator

The Post Increment Operators are the operators that are used as a suffix to its variable. For example, a++ will also increase the value of the variable a by 1. This operator first assign then increment.

Pre-Increment Operator

The Pre-Increment Operators are the operators which are used as a prefix to its variable. For example, ++a will increase the value of the variable a by 1. This operator first increment then assign.

Decrement ( – – ) Operator

The decrement operator is again classified into two categories.

  • Post-Decrement Operator
  • Pre-Decrement Operator
Post-Decrement Operator

The Post Decrement Operators are the operators that are used as a suffix to its variable. For example, a– will also decrease the value of the variable a by 1. This operator first assign then decrements.

Pre-Decrement Operator

The Pre-Decrement Operators are the operators which are used as a prefix to its variable. For example, –a will decrease the value of the variable a by 1. This operator first decrement then assign.

C# Program to Explain Unary Operators

using System;
namespace UnaryOperatorsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Post-Increment
            int x = 10;
            int Result1 = x++;
            Console.WriteLine("Increment Operator");
            Console.WriteLine("x is {0} and Result1 is {1}", x, Result1);

            // Pre-Increment 
            int y = 10;
            int Result2 = ++y;
            Console.WriteLine("y is {0} and Result2 is {1}", y, Result2);
            
            Console.WriteLine("Decrement Operator");
            //Post-Decrement
            int a = 10;
            int Result3 = a--;
            Console.WriteLine("a is {0} and Result3 is {1}", a, Result3);

            // Pre-Decrement 
            int b = 10;
            int Result4 = --b;
            Console.WriteLine("b is {0} and Result4 is {1}", b, Result4);
            Console.ReadKey();
        }
    }
}
Output:
Increment Operator
x is 11 and Result1 is 10
y is 11 and Result2 is 11
Decrement Operator
a is 9 and Result3 is 10
b is 9 and Result4 is 9

Ternary ( ?: ) Operator

In C# Operators, Ternary Operator is also known as Conditional Operator (?:). It is actually the shorthand of the if-else statement. It is called ternary because it has three operands or arguments. The first argument is a comparison argument, the second is the result of a true comparison, and the third is the result of a false comparison. In simple words ternary operator first checks the condition. If the condition is true then it prints the result before ( : ) this symbol else it prints the result after ( : ) this symbol. For example:

using System;
namespace TernaryOperatorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 20, b = 10, res;
            res = ((a > b) ?a : b);  
           //Here a>b condition is true therefore it has return the a as result.
            Console.WriteLine("Result = " + res);

            Console.ReadKey();
        }
    }
}
Output:
Result = 20

Conclusion

In this C# tutorial article I have tried to explain each and every type of csharp(C#) Operators in detail. In my next article I am going to explain the Control Flow Statements in C# Programming language.