What is operator overloading?

Posted by Hanishani under Interview Questions on 7/15/2013 | Points: 10 | Views : 1506 | Status : [Member] | Replies : 1
Can any one please explain me about operator overloading in C#.NET with an example....




Responses

Posted by: Satyapriyanayak on: 7/15/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Overloaded operators sometimes improve program syntax. With the operator keyword, public static operator methods used by the compiler when the designated operators are encountered.


Example


This program declares a Widget class. Here, Widgets can be added together or incremented with the same syntax used for integers. In the Widget class, we provide two public static methods: operator +, and operator ++.

These two methods return an instance of Widget. They receive two or one formal parameters depending on whether the operator is binary (+) or unary (++). They are overloaded operator implementations.

Program that uses operator keyword: C#

using System;

class Widget
{
public int _value;

public static Widget operator +(Widget a, Widget b)
{
// Add two Widgets together.
// ... Add the two int values and return a new Widget.
Widget widget = new Widget();
widget._value = a._value + b._value;
return widget;
}

public static Widget operator ++(Widget w)
{
// Increment this widget.
w._value++;
return w;
}
}

class Program
{
static void Main()
{
// Increment widget twice.
Widget w = new Widget();
w++;
Console.WriteLine(w._value);
w++;
Console.WriteLine(w._value);

// Create another widget.
Widget g = new Widget();
g++;
Console.WriteLine(g._value);

// Add two widgets.
Widget t = w + g;
Console.WriteLine(t._value);
}
}

Output

1
2
1
3

The process of creating more than one method in a class with same name or creating a method in derived class with same name as a method in base class is called as method overloading.


In VB.net when you are overloading a method of the base class in derived class, then you must use the keyword “Overloads”.


But in C# no need to use any keyword while overloading a method either in same class or in derived class.


While overloading methods, a rule to follow is the overloaded methods must differ either in number of arguments they take or the data type of at least one argument.

using System;

namespace ProgramCall
{

class Class1
{

public int Sum(int A, int B)
{
return A + B;
}

public float Sum(int A, float B)
{
return A + B;
}
}

class Class2 : Class1
{
public int Sum(int A, int B, int C)
{
return A + B + C;

}
}

class MainClass
{
static void Main()
{

Class2 obj = new Class2();

Console.WriteLine(obj.Sum(10, 20));

Console.WriteLine(obj.Sum(10, 15.70f));

Console.WriteLine(obj.Sum(10, 20, 30));

Console.Read();
}

}
}


Output

30
25.7
60




http://www.codeproject.com/Articles/178309/Operator-Overloading-in-C-NET
http://www.dotnetperls.com/operator

If this post helps you mark it as answer
Thanks

Hanishani, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response