Saturday, September 17, 2011

.NET interview questions: - What is operator overloading?

Operator overloading is a concept of polymorphism where you can redefine
operators like +, - , * etc with additional functionalities.

For instance we can redefine the + functionalities to add objects like obj1 +
obj2. Below is simple code snippet which redefines + operator.

class SomeClass
{
private int someValue;
public SomeClass(int val)
{
someValue = val;
}
public static SomeClass operator +(SomeClass arg1, SomeClass arg2)
{
return new SomeClass(arg1.someValue + arg2.someValue);
}
}

You can now use the + operator to add objects of type someclass as shown in the below code snippet.

Obj = obj1 = obj2;
Also see the following private constructor video: -





Dotnet interview questions and answers for preparation of real time interviews.

Regards,

Get more from author’s blogs for Most asked .NET interview questions

No comments: