🎓 A1Learner
🔍
0 of 28 lessons completed

C# Operator Overloading

Operator overloading gives a type its own meaning for a symbolic operator such as +. The overloaded operator is a public static method declared with the operator keyword.

Adding two Money values

The operator + method builds a new Money from the sum of the cents. The total prints as 200.

using System;

class Program
{
    static void Main(string[] args)
    {
        Money total = new Money(120) + new Money(80);
        Console.WriteLine(total.Cents);
    }
}

class Money
{
    public int Cents;

    public Money(int cents)
    {
        Cents = cents;
    }

    public static Money operator +(Money a, Money b)
    {
        return new Money(a.Cents + b.Cents);
    }
}

Equality with Equals and GetHashCode

Two Money values with the same cents compare as equal. The == test prints True and the != test prints False.

using System;

class Program
{
    static void Main(string[] args)
    {
        Money first = new Money(100);
        Money second = new Money(100);
        Console.WriteLine(first == second);
        Console.WriteLine(first != second);
    }
}

class Money
{
    public int Cents;

    public Money(int cents)
    {
        Cents = cents;
    }

    public static bool operator ==(Money a, Money b)
    {
        return a.Cents == b.Cents;
    }

    public static bool operator !=(Money a, Money b)
    {
        return a.Cents != b.Cents;
    }

    public override bool Equals(object obj)
    {
        return obj is Money other && Cents == other.Cents;
    }

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

Subtraction and comparison operators

The - operator returns the difference in cents. The > operator compares the cents of the two values.

using System;

class Program
{
    static void Main(string[] args)
    {
        Money start = new Money(200);
        Money spent = new Money(50);
        Money left = start - spent;
        Console.WriteLine(left.Cents);
        Console.WriteLine(start > spent);
    }
}

class Money
{
    public int Cents;

    public Money(int cents)
    {
        Cents = cents;
    }

    public static Money operator -(Money a, Money b)
    {
        return new Money(a.Cents - b.Cents);
    }

    public static bool operator >(Money a, Money b)
    {
        return a.Cents > b.Cents;
    }

    public static bool operator <(Money a, Money b)
    {
        return a.Cents < b.Cents;
    }
}

Example

using System;

class Program
{
    static void Main(string[] args)
    {
        Vector sum = new Vector(1, 2) + new Vector(3, 4);
        Console.WriteLine(sum.X);
        Console.WriteLine(sum.Y);
        Console.WriteLine(sum == new Vector(4, 6));
    }
}

class Vector
{
    public int X;
    public int Y;

    public Vector(int x, int y)
    {
        X = x;
        Y = y;
    }

    public static Vector operator +(Vector a, Vector b)
    {
        return new Vector(a.X + b.X, a.Y + b.Y);
    }

    public static bool operator ==(Vector a, Vector b)
    {
        return a.X == b.X && a.Y == b.Y;
    }

    public static bool operator !=(Vector a, Vector b)
    {
        return a.X != b.X || a.Y != b.Y;
    }

    public override bool Equals(object obj)
    {
        return obj is Vector other && X == other.X && Y == other.Y;
    }

    public override int GetHashCode()
    {
        return X.GetHashCode() ^ Y.GetHashCode();
    }
}

Key points

  • An operator method is public and static, and it takes typed parameters.
  • The + operator for Money adds the cents of its two operands.
  • Paired operators ship together, so == comes with != and > comes with <.
  • A type that overloads == also overrides Equals and GetHashCode.
  • Equals and GetHashCode must agree, so equal values compare and hash the same way.

In this example, a Vector holds two components named X and Y. The + operator adds matching components into a new Vector. The sum has components 4 and 6, and it equals a Vector built with those values.

Note

An operator keeps its usual precedence and its usual number of operands.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A class overloads operator ==. What else must it provide?