Introduction to Operator Overloading in C#
Overloading can be defined as a process of defining and implementing the polymorphism technique, which allows the variables or objects in the program to take on various other forms during the code execution. This technique can be used when the method properties are not similar to the type of arguments, different order of execution, when there is more than one method with same name and different properties, etc. This can be achieved in a program in different methods, such as The different number of parameters, different types of parameters, different order of parameters, optional parameters and named arguments.
Various Operator Overloading in C#
There are multiple operators in C#.
- We can overload all the binary operators i.e +, -, *, /, %, &, |, <<, >>.
- We can overload all the unary operators i.e. ++, –, true, false, + , -, ~.
- Some operators like &&, ||,[] ,() cannot be overloaded.
- We can overload relational operators in pairs. These are ==, =, <, >, <= , >= etc.
- We can overload compound operators as they are already overloaded with respect to the binary operator.
So these are some predefined operators which we can overload.
Syntax:
Below is the syntax of implementing operator overloading:
public static classname operator op (parameters)
{
// Code
}
For Unary Operator
public static classname operator op (t)
{
// Code
}
For Binary Operator
public static classname operator op (t1, t2)
{
// Code
}
Operator is the keyword which is used to implement operator overloading. The return type of operator overload can never be void. In operator overloading preference is always given to user-defined implementations rather than predefined implementations. In overloading, overloaded methods should have a different type of arguments, different number of arguments and order of arguments should be different. So operator overloaded methods are same like any other methods. In user-defined implementations, syntax and precedence cannot be modified. In binary operator, .operator’s left one is a member and on the right side, the object is called a parameter.
Examples of Operator Overloading in C#
Below are the examples which show how to implement Operator Overloading concept in C#:
Example #1
Operator Overloading with Unary Operator. In this example, the unary operator is used for overloading. – operator is used in the class named as Example which calls the overload method. As mentioned in below code compiler takes it as operator-(obj);.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OperatorOverloading
{
class Example
{
public int val1, val2;
public Example(int no1, int no2)
{
val1 = no1;
val2 = no2;
}
public Example()
{
}
public static Example operator -(Example eg1)
{
eg1.val1 = -eg1.val1;
eg1.val1 = -eg1.val1;
return eg1;
}
public void Print()
{
Console.WriteLine("value1 =" + val1);
Console.WriteLine("value2 =" + val2);
Console.Read();
}
class Program
{
static void Main(string[] args)
{
Example eg = new Example(30, -60);
eg.Print();
Example eg1 = new Example();
eg1 = -eg;
eg1.Print();
Console.Read();
}
}
}
}
Output:
Example #2
Operator Overloading with Binary Operator. In this example, binary operator is used to show how we can implement operator overloading. + operator is used for adding the objects. Operator receives one parameter. In code, num is an object of the class named as Example to which object is passed. In overloading, operator’s left one is a member and on the right side, the object is called a parameter.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OperatorOverloading
{
class Example
{
public int num;
public Example()
{
num = 0;
}
public Example(int n)
{
num = n;
}
public static Example operator +(Example e1, Example e2)
{
Example e3 = new Example();
e3.num = e1.num + e2.num;
return e3;
}
public void display()
{
Console.WriteLine("{0}", num);
}
}
class Program
{
static void Main(string[] args)
{
Example num = new Example(200);
Example num1 = new Example(300);
Example num2 = new Example();
num2 = num + num1;
num.display();
num1.display();
num2.display();
Console.Read();
}
}
}
Output:
Example #3
Operator Overloading and Inheritance.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OperatorOverloading
{
class Example
{
public int num;
public Example()
{
num = 0;
}
public Example(int n)
{
num = n;
}
public static Example operator +(Example e1, Example e2)
{
Example e3 = new Example();
e3.num = e1.num + e2.num;
return e3;
}
public void display()
{
Console.WriteLine("{0}", num);
}
}
class Example1 : Example
{
private double a;
private double b;
public Example1(double x, double y)
{
a = x;
b = y;
}
public Example1()
{
}
public void Show()
{
Console.WriteLine("{0} {1}", a, b);
}
}
class Program
{
static void Main(string[] args)
{
Example1 num = new Example1(20.0,2.3);
num.Show();
Example1 num1 = new Example1(3.0,3.1);
num1.Show();
Example1 num2 = new Example1();
//num2 = num + num1;
//num2.Show();
Console.Read();
}
}
}
Output:
Overloaded Operators can also be inherited to the derived class. As it is not possible to declared operator in the derived class to hide the declared operator of the base class.
Example #4
Operator Overloading with Equality Operators. In this example, operator overloading is shown using the equality operator. Equality operator is used where we want to do a comparison. We can overload the method in the class to do a comparison. In this code, the value of objects e1 and e2 are the same. But their references are different. The comparison of values of objects are reference-based. In the case of e2 and e3, it refers to the same object.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OperatorOverloading
{
class Example
{
private int a;
private int b;
public Example()
{
}
public Example(int x, int y)
{
a = x;
b = y;
}
public void Display()
{
Console.WriteLine("{0} {1}", a, b);
}
}
class Program
{
public static void Main()
{
Example e1 = new Example(30, 20);
e1.Display();
Example e2 = new Example(30, 20);
e2.Display();
Example e3 = e2;
e3.Display();
if (e1.Equals(e2))
Console.WriteLine("equal");
else
Console.WriteLine("not equal");
if (e2.Equals(e3))
Console.WriteLine("equal");
Console.ReadLine();
}
}
}
Output:
Conclusion
Operator overloading is an important concept. It provides the reusability of operators in multiple operations. In C# we can overload only a defined set of parameters. Not all the languages of .Net support operator overloading. So in C#, it provides additional capabilities in terms of user-defined implementations.
Recommended Articles
This is a guide to Operator Overloading in C#. Here we discuss the Introduction to Operator Overloading in C# and its Examples with Code Implementation. You can also go through our other suggested articles to learn more –