Hi Dudes
Can any one tell me
and what is the real use of operator overloading and below i have make a program can any one explain it.....?
using System;
class Training
{
string course;
int duration;
public Training(string course, int duration)
{
this.course = course;
this.duration = duration;
}
public static int operator +(Training t1, Training t2)
{
return t1.duration - t2.duration;
}
}
class Program
{
public static void Main()
{
Training t1 = new Training("PHP", 4);
Training t2 = new Training("ASP.NET", 4);
int total = t1 + t2;
Console.WriteLine(total);
Console.ReadKey();
}
}

VulpesPosted Jul 5, 2011, 10:10 AM
In your program, the '+' operator is being overloaded in the Training class so that it returns the difference in duration between two Training objects rather than the sum of two numbers or the concatenation of two strings, which is its normal usage.
In the example given the two Training objects each have a duration of 4 and so applying the '+' operator to them gives a duration of zero (an int).
In my view, this would not be a good use for operator overloading in a 'real world' program because the '+' operator does not give a result which you would intuitively expect (overloading '-' would be a better choice). However, when you're learning C#, this is a secondary concern and you can do what you want if it helps you to learn the language :)