Operator Overloading (C# vs Java)

Like C++, C# allows you to overload operators for use on your own classes. This makes it possible for a user-defined data type to look as natural and be as logical to use as a fundamental data type. For example, you might create a new data type called ComplexNumber to represent a complex number, and provide methods that perform mathematical operations on such numbers using the standard arithmetic operators, such as using the + operator to add two complex numbers.

To overload an operator, you write a function that has the name operator followed by the symbol for the operator to be overloaded. For instance, this is how you would overload the + operator:

public static ComplexNumber operator+(ComplexNumber a, ComplexNumber b)

All operator overloads are static methods of the class. Also be aware that if you overload the equality (==) operator, you must overload the inequality operator (!=) as well. The < and > operators, and the <= and >= operators should also be overloaded in pairs.

The full list of operators that can be overloaded is:

  • Unary operators: +, -, !, ~, ++, --, true, false

  • Binary operators: +, -, *, /, %, &, |, ^, <<, >>, ==, !=, >, <, >=, <=

The following code example creates a ComplexNumber class that overloads the + and - operators:

public class ComplexNumber
{
    private int real;
    private int imaginary;

    public ComplexNumber() : this(0, 0)  // constructor
    {
    }

    public ComplexNumber(int r, int i)  // constructor
    {
        real = r;
        imaginary = i;
    }

    // Override ToString() to display a complex number in the traditional format: 
    public override string ToString()
    {
        return(System.String.Format("{0} + {1}i", real, imaginary));
    }

    // Overloading '+' operator: 
    public static ComplexNumber operator+(ComplexNumber a, ComplexNumber b)
    {
        return new ComplexNumber(a.real + b.real, a.imaginary + b.imaginary);
    }

    // Overloading '-' operator: 
    public static ComplexNumber operator-(ComplexNumber a, ComplexNumber b)
    {
        return new ComplexNumber(a.real - b.real, a.imaginary - b.imaginary);
    }
}

This class enables you to create and manipulate two complex numbers with code such as this:

class TestComplexNumber
{
    static void Main()
    {
        ComplexNumber a = new ComplexNumber(10, 12);
        ComplexNumber b = new ComplexNumber(8, 9);

        System.Console.WriteLine("Complex Number a = {0}", a.ToString());
        System.Console.WriteLine("Complex Number b = {0}", b.ToString());

        ComplexNumber sum = a + b;
        System.Console.WriteLine("Complex Number sum = {0}", sum.ToString());

        ComplexNumber difference = a - b;
        System.Console.WriteLine("Complex Number difference = {0}", difference.ToString());
    }
}

As the program demonstrates, you can now use the plus and minus operators on objects belonging to your ComplexNumber class quite intuitively. Here is the output you would get:

Complex Number a = 10 + 12i

Complex Number b = 8 + 9i

Complex Number sum = 18 + 21i

Complex Number difference = 2 + 3i

Java does not support operator overloading, although internally it overloads the + operator for string concatenation.

See Also

Tasks

Operator Overloading Sample

Concepts

C# Programming Guide

Reference

Overloadable Operators (C# Programming Guide)

Other Resources

The C# Programming Language for Java Developers