1.
Introduction
1.1.
1.2.
Syntax
1.3.
1.4.
1.5.
2.
FAQs
3.
Key Takeaways
Last Updated: Mar 27, 2024

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

Overloaded operators are functions that have unique names that begin with the operator keyword and end with the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

Letâ€™s take an example to understand it.

The above example implements the addition operator ( + ) for a user-defined class Room. It adds the attributes of two Room objects and returns the resultant  Room object.

### Syntax

The following is the syntax of operator overloading in the C# language.

Letâ€™s learn Unary and Binary operator overloading in C# with multiple examples.

The following example explains how to overload unary operators in C#

Code:

``````using System;

class Calculator { // calculator class

public int number1 , number2;
public Calculator(int num1 , int num2)
{
number1 = num1;
number2 = num2;
}

// following functions will perform operations by changing
// the sign of integers
public static Calculator operator -(Calculator c1)
{
c1.number1 = -c1.number1;
c1.number2 = -c1.number2;
return c1;
}

// function to display/print the numbers
public void Print()
{
Console.WriteLine ("Num1 = " + number1);
Console.WriteLine ("Num2 = " + number2);
}
}

class TestingInMain
{

// Main Function or Driver code starts here
static void Main(String []args)
{

// with the class object
Calculator calc = new Calculator(28, -12);

calc = -calc;

// printing the result
calc.Print();
}

}
}``````

Output

The sign of the numbers is reversed in the above example of unary operator overloading in C#. Now let's learn binary operators overloading.

Since two operands are required for binary operators, two parameters will be passed to the function for binary operator overloading in C#. The following example explains how to implement binary operator overloading in C#.

Code:

``````using System;

class Calculator { // calculator class

public int number = 0;

// constructor with no-argument
public Calculator() {}

// parameterized constructor
public Calculator(int n)
{
number = n;
}

public static Calculator operator + (Calculator Cal1, Calculator Cal2)
{
Calculator Cal3 = new Calculator(0);
Cal3.number = Cal2.number + Cal1.number;
return Cal3; //returning the result of same type
}

// function to print result
public void Print()
{
Console.WriteLine("{0}", number);
}
}

class TestingInMain
{

// Main Function or Driver code starts here
static void Main(String []args)
{

Calculator num1 = new Calculator(510);
Calculator num2 = new Calculator(220);
Calculator num3 = new Calculator();

num3 = num1 + num2;

num1.Print(); // Prints 510
num2.Print(); // Prints 220
num3.Print(); // Prints the result i.e. 730
}

}
}``````

Output:

In the above example, the result is 510+220, which is 730. Let's learn some benefits of operator overloading in C#.

•  When C# operators are applied to user-defined data types, they gain additional capabilities because of operator overloading.
• Operators can be thought of as compiler-internal functions.
• The main benefit of operator overloading is that it allows us to integrate a new class type into our programming environment seamlessly.

This concludes operator overloading in C#. Now letâ€™s move on to the FAQ section.

Also see, Ienumerable vs Iqueryable

## FAQs

The method to assign more than one operation on the same operator is known as operator overloading.
2. Can we overload comparison operators?
Yes, we can overload comparison operators, but they can not be overloaded directly.
3. What is a binary operator?
A binary operator is an operator that takes two operands to perform operations.
As the name suggests, there is only one operand in unary operator overloading.
5. What are access specifiers in C#
Access specifier or modifier is the keyword used to specify the accessibility of a type and its members. The public, private, and internal access specifiers are mainly used in C#.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp