Binary Operator Overloading in C#

 

As of now we discuss about method overloading in C#. today we discuss about how we can overload binary operators in C#. We will discuss about Binary operator overloading by considering plus(+) binary operator.

 

The plus(+) operator behaves differently based on type of data. For example if we apply plus(+) operator on two integers it produces the sum of those two integers as shown below.

           

            int i = 10;

            int j = 20;

            int k = i + j;//the value of the variable k is 30

 

If we apply same plus(+) operator on two string variables it will do concatenation of those two string variables as shown below.

          

            string str1 = "This is ";

            string str2 = "Example of Binary Operator Overloading";

 

            string str3 = str1 + str2;//the value of the variable str3 is "This is Example of Binary Operator Overloading"

 

But if you want to apply this binary operator on custom type field, you have to define your own behaviour according to your requirement. Here we discuss about binary operator overloading which supports addition of our custom type Point.

 

Let's have some Point class which expects X and Y arguments while creating object as shown below.

 

namespace BinaryOperatorOverloadingExp

{

    public class Point

    {

        private int x, y;

        public Point(int xPos, int yPos)

        {

            x = xPos;

            y = yPos;

        }

 

        public int GetX()

        {

            return x;

        }

 

        public int GetY()

        {

            return y;

        }

    }

}

 

Create two objects for Point class and try to add two objects as shown below.

           

            Point point1 = new Point(10, 20);

            Point point2 = new Point(20, 30);

 

            Point point3 = point1 + point2;

 

In the above when we are trying to add Point objects to sum the corresponding  X, Y and expected result is X=30, Y=50; But we will get errors because plus(+) operator won't support adding the Point type. So to support that we have to overload plus(+) operator as shown below in Point class.

 

namespace BinaryOperatorOverloadingExp

{

    public class Point

    {

        private int x, y;

        public Point(int xPos, int yPos)

        {

            x = xPos;

            y = yPos;

        }

 

        //overloading binary operator

        public static Point operator +(Point p1, Point p2)

        {

            return new Point(p1.x + p2.x, p1.y + p2.y);

        }

 

        public int GetX()

        {

            return x;

        }

 

        public int GetY()

        {

            return y;

        }

    }

}

 

Then try to add two Point type objects again to get the third Point as shown below.

           

            Point point1 = new Point(10, 20);

            Point point2 = new Point(20, 30);

 

            Point point3 = point1 + point2;

 

            Console.WriteLine("X = {0} ; Y = {1}", point3.GetX(), point3.GetY());

            Console.ReadLine();

 

Now you won't get any compile-time errors if you execute the code and the out put is as shown below.

         

                                                                                                                  

                                                                                                        BinaryOperatorOverloadingExp.zip (25.21 kb)