Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C#

Learn step by step



Operator Overloading

Operator overloading is one of the good feature of Object Oriented Programming.C# supports the operator overloading concepts.Operators in C# can be defined to work with the user-defined data types such as structs and classes .It Works the same way as the Built in types.
There are some operators in C# that can be overloaded.

  • Overloadable operators
  • Non Overloadable operators

OVERLOADABLE OPERATORS

Operators that can be overloaded in c# as shown below:


NON OVERLOADABLE OPERATORS

Operators that can be overloaded in C# as shown below:

Note::- Logical operators must be overloaded in pairs.Ex.==,!= etc.

  • Binary operators and its compound assignment equivalent is implicitly overloaded.
  • The operators that are not defined in C# can not overloaded.

Need for Operator overloading

There are some places where mostly operators overloading are used.

  • Mathematical or Physical modeling
  • Graphical programs
  • Text manipulations
  • Financial programs etc.

Defining Operator Overloading

This is done with the help of special method called operator method ,which describe the task.The operator is defined in such a way as a Method,we use Operator keyword to define the operator method.

There are some feature of operators methods which is give below:

  • It can be public or static.
  • The return value is the type that can be any type. 
  • In Unary operators ,the argument must be the same type as that of the enclosing class or struct.
  • The Number of arguments will be one for the Unary operators and two for the binary operators.
  • In binary operators,First argument must be of the same type as that of the enclosing class or struct and second can be of any type.

There are three types of Overloading in C#

  • Unary operators overloading
  • Binary operators overloading
  • Comparison operators overloading

Unary operators overloading

I have shown how to overload an Unary Operator in given example which is shown below:


using System;
class bank
{
int x;
int y;
public bank(int a, int b)
{
x = a;
y = b;
}
public bank()
{
}
public void display()
{
Console.Write(" " + x);
Console.Write(" " + y);
Console.WriteLine();
}
public static bank operator -(bank b)
{
b.x = -b.x;
b.y = -b.y;
return b;
}
}
class program
{
public static void Main()
{
bank ba1 = new bank(10,-20);
ba1.display();
bank ba2 = new bank();
ba2.display();
ba2 = -ba1;
ba2.display();
Console.ReadLine();
}
}

Output

10 -20
0 0
-10 20

In above program ,the method operator -() takes one argument of type bank and changes the sign of data members of the object b ,since b is a member of the same class ,so it can directly access the members of the object which activate it.since operator method return an object so this statement ba2 == - ba1 is true.If Method does not return any object then it will not true.

Binary Operators Overloading

using System;
class bank
{
int x;
int y;
public bank(int a, int b)
{
x = a;
y = b;
}
public bank()
{
}
public void display()
{
Console.Write(" " + x);
Console.Write(" " + y);
Console.WriteLine();
}
public static bank operator -(bank b)
{
b.x = -b.x;
b.y = -b.y;
return b;
}
}
class program
{
public static void Main()
{
bank ba1 = new bank(10,-20);
ba1.display();
bank ba2 = new bank();
ba2.display();
ba2 = -ba1;
ba2.display();
Console.ReadLine();
}
}

Output

p=10+j2
q=20+j15.5
r=30+j-5.5

Note: The Method operator + () takes two argument (ComplexNumber) and add the two object in same class.

Comparison Operators Overloading

There are six comparison operators that can be considered in three pairs:

  • == and !=
  • < and <=
  • > and >=

The Significance of these two pairing:

  • Each pair ,the second operator should always give exactly the opposite result to the first.
  • We must require us to overload the comparison operators in pairs.


using System;
namespace comparison
{

class Vector
{
int x, y, z;
public Vector(int p, int q, int r)
{
x = p;
y = q;
z = r;
}
public static bool operator ==(Vector v1, Vector v2)
{
if (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z)
return (true);
else
return (false);
}
public static bool operator !=(Vector v1, Vector v2)
{
return (!(v1 == v2));
}
}
class comparison
{
static void Main()
{
Vector v1 = new Vector(10, 20, 30);
Vector v2 = new Vector(40, 50, 60);

if (v1 == v2)
Console.WriteLine("v1 and v2 both are Equal");
else
Console.WriteLine("v1 and v2 are not equal");

Console.ReadLine();
}
}
}

Output

v1 and v2 are not equal

Note:Comparison operators must return a bool type value. But other operators (Unary,Binary) can return any type of value.



Related Videos