Operator overloading in C#

By
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.
  1. Overloadable operators
  2. Non Overloadable operators

     1. ) Overloadable operators:-

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

     2.) 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.
Example:
Unary minus:
Public static bank operator -(bank b)

Vector addition(Binary operator)
Public static Vector operator -(Vector x ,Vector y)

Comparison Operator
Public Static Vector operator ==(Vector x,Vector y)

Description:-  

Vector is a Data type of class that represent magnitude and direction both.
There are some steps ,Process of Overloading in C#.
  • Create  a class or struct that defines the Data type that can be used in the overloading operations.
  • Declare the operator Method operator - () Using public and static specifier.
  • Define the body of the operator method to implement the required operations.
There are three types of Overloading in C#.
  1. Unary operators overloading
  2. Binary operators overloading
  3. Comparison operators overloading

     1. ) 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:-

Description:- 

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.

     2. ) Binary Operators Overloading:-

In above example we we have seen how to overload an Unary operator in C#.We can use same Mechanism to overload binary the Binary operators in C#

using System;
namespace binary_overload
{
    class complexNumber
    {
        int x;
        double y;
        public complexNumber(int real, double imagnary)
        {
            x = real;
            y = imagnary;
        }
        public complexNumber()
        {
        }
 public static complexNumber operator + (complexNumber c1, complexNumber c2)
        {
            complexNumber c = new complexNumber();
            c.x=c1.x+c2.x;
            c.y=c1.x-c2.y;
            return c;
        }
        public void show()
        {
            Console.Write(x);
            Console.Write("+j"+y);
            Console.WriteLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            complexNumber p, q, r;
            p = new complexNumber(10, 2.0);
            q = new complexNumber(20, 15.5);
            r = p + q;
            Console.Write("p=");
            p.show();
            Console.Write("q=");
            q.show();
            Console.Write("r=");
            r.show();
            Console.ReadLine();
        }
    }
}
Output:

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

     3. ) 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:
Note:-
Comparison operators must return a bool type value. But other operators (Unary,Binary) can return any type of value.
For more:-
  1. Method overloading
  2. Method overiding
  3. Method Hiding
  4. oops 
  5. Interface
  6. Delegate
  7. Event
  8. Reflection concepts
  9. File and File info class concepts
  10. Real File handling concepts with example
  11. Collection concepts in c#
  12. Directory and Directory info class in c#

To Get the Latest  Free Updates Subscribe
            download attached whole Application
                        Download

0 comments:

Post a Comment

Powered by Blogger.