Wednesday, February 1, 2012

C # Operator Overloading


When creating certain classes such as BigInteger using methods for certain operations can be very frustrating and make your code very hard to follow.
Consider this Java program for calculating the discriminate for use in calculating the x-intercepts of a quadratic function.
Code:
 BigInteger discriminate = b.pow(2).add(c.multiply(a).multiply(new BigInteger("4")).negate());
What this does is it multiplies b by itself once and then calculates c * a * 4 and adds the negation of that to b squared. Note: there is no subtract method hence the use of negate.

This is just really ugly as we have lots of nested method calls together. Sure we could break this up into multiple lines by it would be nice if we could just write b* b – 4*a*c. Since Java doesn’t let you define your own operators for objects we cannot do this. C#, however, does support this. It is called operator overloading.

Creating a class

First, we are going to create a point class to use in our demonstration of how to override operators.
Code:
  class Point    {
        public int x { get; set; }
        public int y { get; set; }

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
This class simply as two values x and y. When we are adding two points together what we want to do is add the x values together and add the y values together and add these two values together.
So if we have Point1 = (x1, y1) and Point2 = (x2, y2) then Point1 + Point2 = x1 + y1 + x2 + y2. We could create a static method to do this but then we run into the same problems with the above Java example. Instead let us overload the + operator.

Overloading Operators

To overload an operator we write something similar to a method. The method we are going to write must be declared static and one of the parameters it takes must be of the type the operator is defined in.
Example:
Code:
  public static int operator +(Point one, Point two)
        {
            return one.x + one.y + two.x + two.y;
        }
This code if you notice is declared static and is an operator (+) that returns an integer. The operator can applied to two points. This cannot be applied to three or more points.
Example of using the operator:
Code:
  var one = new Point(3,2);
var two = new Point(4, 5);
Console.WriteLine(one + two);
The output is:
14
Another task we might like to do is add two or more points together and make a new Point. Change the operator to this:
Code:
 public static Point operator +(Point one, Point two)
        {
            return new Point(one.x + two.x,one.y + two.y);
        }
We can now add a lot more than two points together as the following example demonstrates.
Code:
   var one = new Point(3,2);
            var two = new Point(4, 5);
            var three = new Point(6, 7);

            var result = one + two + three;
            Console.WriteLine(result.x + " " + result.y);
The output is:
13 14

What really is happening here is we are doing one + two which returns a new point. Then this new point is added to three which produces a new point. So in execution of this program five points are created.

Post if you have any questions below!

No comments:

Post a Comment