This sample post will help you to get hands on experience with operator overloading in C#.
Operator overloading give a different meaning to existing operator and enhance the functionality to work with other rich objects etc.
like in this given example we have used plus(+) operator to work with object of the class and it gives the result after adding the variable of the class.
using System;
using System.Collections.Generic;
using System.Text;
namespace Console_App
{
class Program
{
static void Main(string[] args)
{
OpOverloading a = new OpOverloading(20);
OpOverloading b = new OpOverloading(25);
OpOverloading c;
c = a + b;
System.Console.WriteLine(c.i);
Console.ReadLine();
}
}
public class OpOverloading
{
public int i;
public OpOverloading(int j)
{
i = j;
}
public static OpOverloading operator +(OpOverloading x, OpOverloading y)
{
System.Console.WriteLine("operator + " + x.i + " " + y.i);
OpOverloading z = new OpOverloading(x.i + y.i);
return z;
}
}
}