C# | Operators are the basis of any programming language. Therefore, the functionality of the C# language is incomplete without the use of operators. Operators allow you to perform different types of operations on operands. In C#, operators can be classified based on different functionality.

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Conditional Operator

In C#, Operators can also categorize based upon Number of Operands: 

  • Unary Operator: Operator that takes one operand to perform the operation.
  • Binary Operator: Operator that takes two operands to perform the operation.
  • Ternary Operator: Operator that takes three operands to perform the operation.
     

Arithmetic Operators

These are used to perform arithmetic/mathematical operations on operands. The Binary Operators falling in this category are:

  • Addition: The ‘+’ operator adds two operands. For example, x+y.
  • Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y.
  • Multiplication: The ‘*’ operator multiplies two operands. For example, x*y.
  • Division: The ‘/’ operator divides the first operand by the second. For example, x/y.
  • Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For example, x%y.

Example:

// working of Binary Arithmetic Operators
using System;
namespace Arithmetic
{
	class CodeConfig
	{
		// Main Function
		static void Main(string[] args)
		{
			int result;
			int x = 10, y = 5;
			
			// Addition
			result = (x + y);
			Console.WriteLine("Addition Operator: " + result);
			
			// Subtraction
			result = (x - y);
			Console.WriteLine("Subtraction Operator: " + result);
			
			// Multiplication
			result = (x * y);
			Console.WriteLine("Multiplication Operator: "+ result);
			
			// Division
			result = (x / y);
			Console.WriteLine("Division Operator: " + result);
			
			// Modulo
			result = (x % y);
			Console.WriteLine("Modulo Operator: " + result);
		}
	}
}

Output:

Addition Operator: 15
Subtraction Operator: 5
Multiplication Operator: 50
Division Operator: 2
Modulo Operator: 0

The ones falling into the category of Unary Operators are:

  • Increment: The ‘++’ operator is used to increment the value of an integer. When placed before the variable name (also called pre-increment operator), its value is incremented instantly. For example, ++x
    And when it is placed after the variable name (also called post-increment operator), its value is preserved temporarily until the execution of this statement, and it gets updated before the execution of the next statement. For example, x++.
  • Decrement: The ‘- -‘ operator is used to decrement the value of an integer. When placed before the variable name (also called pre-decrement operator), its value is decremented instantly. For example, – -x.
    And when it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until the execution of this statement, and it gets updated before the execution of the next statement. For example, x- –.

Example:

// C# program to demonstrate the working 
// of Unary Arithmetic Operators
using System;
namespace Arithmetic {
	
	class CodeConfig {
		
		// Main Function
		static void Main(string[] args)
		{
			
			int a = 10, res;

			// post-increment example:
			// res is assigned 10 only, 
			// a is not updated yet
			res = a++;
			
			//a becomes 11 now
			Console.WriteLine("a is {0} and res is {1}", a, res);
		
		
			// post-decrement example:
			// res is assigned 11 only, a is not updated yet
			res = a--;
			
			//a becomes 10 now
			Console.WriteLine("a is {0} and res is {1}", a, res); 
		
		
			// pre-increment example:
			// res is assigned 11 now since a
			// is updated here itself
			res = ++a;
			
			// a and res have same values = 11
			Console.WriteLine("a is {0} and res is {1}", a, res);
		
		
			// pre-decrement example:
			// res is assigned 10 only since
			// a is updated here itself
			res = --a;
			
			// a and res have same values = 10
			Console.WriteLine("a is {0} and res is {1}",a, res); 
		
			
		}
	}
}

Output:

a is 11 and res is 10
a is 10 and res is 11
a is 11 and res is 11
a is 10 and res is 10

Relational Operators

// C# program to demonstrate the working 
// of Relational Operators
using System;
namespace Relational {
	
class CodeConfig {
	
	// Main Function
	static void Main(string[] args)
	{
		bool result;
		int x = 5, y = 10;
		
		// Equal to Operator
		result = (x == y);
		Console.WriteLine("Equal to Operator: " + result);
		
		// Greater than Operator
		result = (x > y);
		Console.WriteLine("Greater than Operator: " + result);
		
		// Less than Operator
		result = (x < y);
		Console.WriteLine("Less than Operator: " + result);
		
		// Greater than Equal to Operator
		result = (x >= y);
		Console.WriteLine("Greater than or Equal to: "+ result);
		
		// Less than Equal to Operator
		result = (x <= y);
		Console.WriteLine("Lesser than or Equal to: "+ result);
		
		// Not Equal To Operator
		result = (x != y);
		Console.WriteLine("Not Equal to Operator: " + result);
	}
}
}

Relational operators are used for comparison of two values. Let’s see them one by one:

  • ‘=='(Equal To) operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.
  • ‘!='(Not Equal To) operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
  • ‘>'(Greater Than) operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.
  • ‘<‘(Less Than) operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6<5 will return false.
  • ‘>='(Greater Than Equal To) operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return true.
  • ‘<='(Less Than Equal To) operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return true.

Output:

Equal to Operator: False
Greater than Operator: False
Less than Operator: True
Greater than or Equal to: False
Lesser than or Equal to: True
Not Equal to Operator: True

Logical Operators

They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They are described below:

  • Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
  • Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.
  • Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.

Example:

// C# program to demonstrate the working 
// of Logical Operators
using System;
namespace Logical {
	
class CodeConfig{
	
	// Main Function
	static void Main(string[] args)
	{
			bool a = true,b = false, result;
		
			// AND operator
			result = a && b;
			Console.WriteLine("AND Operator: " + result);
			
			// OR operator
			result = a || b;
			Console.WriteLine("OR Operator: " + result);
			
			// NOT operator
			result = !a;
			Console.WriteLine("NOT Operator: " + result);
		
	}
}
}

Output:

AND Operator: False
OR Operator: True
NOT Operator: False

Bitwise Operators

In C#, there are 6 bitwise operators which work at bit level or used to perform bit by bit operations. Following are the bitwise operators:

  • & (bitwise AND) Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
  • | (bitwise OR) Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
  • ^ (bitwise XOR) Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
  • << (left shift) Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
  • >> (right shift) Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

Example:

// C# program to demonstrate the working 
// of Bitwise Operators
using System;
namespace Bitwise {
	
class CodeConfig{
	
	// Main Function
	static void Main(string[] args)
	{
		int x = 5, y = 10, result;
		
			// Bitwise AND Operator
			result = x & y;
			Console.WriteLine("Bitwise AND: " + result);
			
			// Bitwise OR Operator
			result = x | y;
			Console.WriteLine("Bitwise OR: " + result);
			
			// Bitwise XOR Operator
			result = x ^ y;
			Console.WriteLine("Bitwise XOR: " + result);
			
			// Bitwise AND Operator
			result = ~x;
			Console.WriteLine("Bitwise Complement: " + result);
			
			// Bitwise LEFT SHIFT Operator
			result = x << 2;
			Console.WriteLine("Bitwise Left Shift: " + result);
			
			// Bitwise RIGHT SHIFT Operator
			result = x >> 2;
			Console.WriteLine("Bitwise Right Shift: " + result);
		
	}
}
}

Output :

Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise Complement: -6
Bitwise Left Shift: 20
Bitwise Right Shift: 1

Assignment Operators

Assignment operators are used to assigning a value to a variable. The left side operand of the assignment operator is a variable and right-side operand of the assignment operator is a value. The value on the right side must be of the same datatype of the variable on the left side otherwise the compiler will raise an error.

Different types of assignment operators are shown below:

  • “=”(Simple Assignment): This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.
    Example:

a = 10; b = 20; ch = ‘y’;

  • “+=”(Add Assignment): This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
    Example:

(a += b) can be written as (a = a + b)

If initially value stored in a is 5. Then (a += 6) = 11.

  • “-=”(Subtract Assignment): This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.
    Example:

(a -= b) can be written as (a = a – b)

If initially value stored in a is 8. Then (a -= 6) = 2.

  • “*=”(Multiply Assignment): This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
    Example:

(a *= b) can be written as (a = a * b)

If initially value stored in a is 5. Then (a *= 6) = 30.

  • “/=”(Division Assignment): This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
    Example:

(a /= b) can be written as (a = a / b)

If initially value stored in a is 6. Then (a /= 2) = 3.

  • “%=”(Modulus Assignment): This operator is combination of ‘%’ and ‘=’ operators. This operator first modulo the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
    Example:

(a %= b) can be written as (a = a % b)

If initially value stored in a is 6. Then (a %= 2) = 0.

  • “<<=”(Left Shift Assignment) : This operator is combination of ‘<<‘ and ‘=’ operators. This operator first Left shift the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
    Example:

(a <<= 2) can be written as (a = a << 2)

If initially value stored in a is 6. Then (a <<= 2) = 24.

  • “>>=”(Right Shift Assignment) : This operator is combination of ‘>>’ and ‘=’ operators. This operator first Right shift the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
    Example:

(a >>= 2) can be written as (a = a >> 2)

If initially value stored in a is 6. Then (a >>= 2) = 1.

  • “&=”(Bitwise AND Assignment): This operator is combination of ‘&’ and ‘=’ operators. This operator first “Bitwise AND” the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
    Example:

(a &= 2) can be written as (a = a & 2)

If initially value stored in a is 6. Then (a &= 2) = 2.

  • “^=”(Bitwise Exclusive OR): This operator is combination of ‘^’ and ‘=’ operators. This operator first “Bitwise Exclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
    Example:

(a ^= 2) can be written as (a = a ^ 2)

If initially value stored in a is 6. Then (a ^= 2) = 4.

  • “|=”(Bitwise Inclusive OR) : This operator is combination of ‘|’ and ‘=’ operators. This operator first “Bitwise Inclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
    Example :

(a |= 2) can be written as (a = a | 2)

If initially, value stored in a is 6. Then (a |= 2) = 6.

Example:

//working of Assignment Operators
using System;
namespace Assignment {
	
public class CodeConfig {
	static void Main(string[] args)
	{
		
			// initialize variable x
			// using Simple Assignment 
			// Operator "="
			int x = 15; 
			
			// it means x = x + 20
			x += 20; 
			Console.WriteLine("Add Assignment Operator: " + x);
			
			// initialize variable x again
			x = 20;
			
			// it means x = x - 10
			x -= 10; 
			Console.WriteLine("Subtract Assignment Operator: " + x);
			
			// initialize variable x again
			x = 15;
			
			// it means x = x * 5
			x *= 5; 
			Console.WriteLine("Multiply Assignment Operator: " + x);
			
			// initialize variable x again
			x = 25;
			
			// it means x = x / 5
			x /= 5; 
			Console.WriteLine("Division Assignment Operator: " + x);
			
			// initialize variable x again
			x = 25;
			
			// it means x = x % 5
			x %= 5; 
			Console.WriteLine("Modulo Assignment Operator: " + x);
			
			// initialize variable x again
			x = 8;
			
			// it means x = x << 2
			x <<= 2; 
			Console.WriteLine("Left Shift Assignment Operator: " + x);
			
			// initialize variable x again
			x = 8;
			
			// it means x = x >> 2
			x >>= 2; 
			Console.WriteLine("Right Shift Assignment Operator: " + x);
			
			// initialize variable x again
			x = 12;
			
			// it means x = x >> 4
			x &= 4; 
			Console.WriteLine("Bitwise AND Assignment Operator: " + x);
			
			// initialize variable x again
			x = 12;
			
			// it means x = x >> 4
			x ^= 4; 
			Console.WriteLine("Bitwise Exclusive OR Assignment Operator: " + x);
			
			// initialize variable x again
			x = 12;
			
			// it means x = x >> 4
			x |= 4; 
			Console.WriteLine("Bitwise Inclusive OR Assignment Operator: " + x);
		
	}
}
}

Output :

Add Assignment Operator: 35
Subtract Assignment Operator: 10
Multiply Assignment Operator: 75
Division Assignment Operator: 5
Modulo Assignment Operator: 0
Left Shift Assignment Operator: 32
Right Shift Assignment Operator: 2
Bitwise AND Assignment Operator: 4
Bitwise Exclusive OR Assignment Operator: 8
Bitwise Inclusive OR Assignment Operator: 12

Conditional Operator

It is ternary operator which is a shorthand version of if-else statement. It has three operands and hence the name ternary. It will return one of two values depending on the value of a Boolean expression. 

Syntax: condition? first_expression : second_expression;

Explanation:
condition: It must be evaluated to true or false.
If the condition is true
first_expression is evaluated and becomes the result. 
If the condition is false, 
second_expression is evaluated and becomes the result. 

Example:


// working of Conditional Operator
using System;
namespace Conditional {
	
class CodeConfig{
	
	// Main Function
	static void Main(string[] args)
	{
			int x = 5, y = 10, result;
			
			// To find which value is greater
			// Using Conditional Operator
			result = x > y ? x : y; 
			
			// To display the result 
			Console.WriteLine("Result: " + result);
			
			// To find which value is greater
			// Using Conditional Operator
			result = x < y ? x : y; 
			
			// To display the result
			Console.WriteLine("Result: " + result);
	}
}
}

Output:

Result: 10
Result: 5

In this article we tried to explain C# | Operators with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles.

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can