Overloading in C#

This article has been excerpted from book "The Complete Visual C# Programmer's Guide from the Authors of C# Corner".

C# allows user-defined types to overload operators by defining static member functions using the operator keyword. The operator keyword is used to declare an operator in a class or struct declaration. Not all operators can be overloaded, and some that can be overloaded have certain restrictions, as listed in Table 5.7.

table5.7.gif

Listing 5.54 illustrates overloading on complex numbers.

Listing 5.54: Operator Overloading Example


// Operator overloading your custom classes in C#
// OperatorOverloading Complex Numbers


using
System;

public
class Complex
{
    public int real = 0;
    public int imaginary = 0;
    public Complex(int real, int imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }

    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
    }

    public static void Main()
    {
        Complex num1 = new Complex(2, 3);
        Complex num2 = new Complex(3, 4);
        Complex sum = num1 + num2;
        Console.WriteLine("Real: {0}", sum.real);
        Console.WriteLine("Imaginary: {0}", sum.imaginary);
        Console.ReadLine();
    }
}


Figure 5.14 shows the resulting display.

Figure5.14.gif

Figure 5.14: Screen Output Generated from Listing 5.54

Listing 5.55 presents a more sophisticated example of operator overloading.

Listing 5.55: Sophisticated Operator Overloading Example


// Operator overloading

using
System;

class
Rectangle
{
    private int iHeight;
    private int iWidth;
    public Rectangle()
    {
        Height = 0;
        Width = 0;
    }

    public Rectangle(int w, int h)
    {
        Width = w;
        Height = h;
    }

    public int Width
    {
        get
        {
            return iWidth;
        }

        set
        {
            iWidth = value;
        }
    }

    public int Height
    {
        get
        {
            return iHeight;
        }

        set
        {
            iHeight = value;
        }
    }

    public int Area
    {
        get
        {
            return Height * Width;
        }
    }

    /* Overloading == */
    public static bool operator ==(Rectangle a, Rectangle b)
    {
        return ((a.Height == b.Height) && (a.Width == b.Width));
    }

    /* Overloading != */
    public static bool operator !=(Rectangle a, Rectangle b)
    {
        return !(a == b);
    }

    /* Overloading > */
    public static bool operator >(Rectangle a, Rectangle b)
    {
        return a.Area > b.Area;
    }

    /* Overloading < */
    public static bool operator <(Rectangle a, Rectangle b)
    {
        return !(a > b);
    }

    /* Overloading >= */
    public static bool operator >=(Rectangle a, Rectangle b)
    {
        return (a > b) || (a == b);
    }

    /* Overloading <= */
    public static bool operator <=(Rectangle a, Rectangle b)
    {
        return (a < b) || (a == b);
    }

    public override bool Equals(object o)
    {
        return this.Equals(o);
    }

    public override int GetHashCode()
    {
        return this.GetHashCode();
    }

    public override String ToString()
    {
        return "Height=" + Height + ",Width=" + Width;
    }

    public static void Main()
    {
        Rectangle objRect1 = new Rectangle();
        Rectangle objRect2 = new Rectangle();
        Rectangle objRect3 = new Rectangle(10, 15);
        objRect1.Height = 15;
        objRect1.Width = 10;
        objRect2.Height = 25;
        objRect2.Width = 10;
        Console.WriteLine("Rectangle#1 " + objRect1);
        Console.WriteLine("Rectangle#2 " + objRect2);
        Console.WriteLine("Rectangle#3 " + objRect3);

        if (objRect1 == objRect2)
        {
            Console.WriteLine("Rectangle1 & Rectangle2 are Equal.");
        }
        else
        {
            if (objRect1 > objRect2)
            {
                Console.WriteLine("Rectangle1 is greater than Rectangle2");
            }
            else
            {
                Console.WriteLine("Rectangle1 is lesser than Rectangle2");
            }
        }
        if (objRect1 == objRect3)
        {
            Console.WriteLine("Rectangle1 & Rectangle3 are Equal.");
        }
        else
        {
            Console.WriteLine("Rectangle1 & Rectangle3 are not Equal.");
        }
        Console.ReadLine();
    }
}


Figure 5.15 contains the screen output from this example.

Figure5.15.gif

Figure 5.15: Screen Output Generated from Listing 5.55

Conclusion


Hope this article would have helped you in understanding Overloading in C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.


Similar Articles
C# Corner
C# Corner started as an online community for software developers in 1999.