Mastering C# Fundamentals: Operator Overloading and Class Inheritance

Implementing Operator Overloading in C#

Operator overloading allows developers to define how operators (such as +, -, *, etc.) behave when applied to user-defined types. This feature is particularly useful for creating intuitive APIs when working with mathematical structures or custom data containers.

The following example demonstrates a Container class that overloads the + operator to combine the dimensions of two objects.

using System;

namespace AdvancedOOP
{
    class Container
    {
        public double Length { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }

        public double GetTotalVolume()
        {
            return Length * Width * Height;
        }

        // Overloading the '+' operator
        public static Container operator +(Container first, Container second)
        {
            return new Container
            {
                Length = first.Length + second.Length,
                Width = first.Width + second.Width,
                Height = first.Height + second.Height
            };
        }
    }

    class Program
    {
        static void Main()
        {
            Container smallBox = new Container { Length = 5.0, Width = 4.0, Height = 3.0 };
            Container largeBox = new Container { Length = 10.0, Width = 8.0, Height = 6.0 };

            Console.WriteLine($"Volume of small box: {smallBox.GetTotalVolume()}");
            Console.WriteLine($"Volume of large box: {largeBox.GetTotalVolume()}");

            // Using the overloaded '+' operator
            Container combinedBox = smallBox + largeBox;

            Console.WriteLine($"Combined Volume: {combinedBox.GetTotalVolume()}");
        }
    }
}

Key Concepts of Operator Overloading

When overloading operators in C#, there are several rules to observe:

  • Static Requirement: Operator declarations must be public and static.
  • Parameter Count: Binary operators (like +) must take exactly two parameters, and atleast one must be of the containing class type.
  • Logic Consistency: Overloading an operator does not automatically overload its compound assignment counterpart (e.g., overloading + does not explicitly overload +=, though += will use the custom + logic).

Inheritance and Polymorphism in C#

Inheritance allows a class to inherit members from a base class, promoting code reuse. Polymorphism enables a single interface to represent different underlying forms (data types).

Consider a scenario where different shapes share common properties but calculate volume differently:

public class Shape
{
    public string Name { get; set; }
    
    public virtual void DisplayInfo()
    {
        Console.WriteLine($"This is a {Name}.");
    }
}

public class Cube : Shape
{
    public double Side { get; set; }

    public override void DisplayInfo()
    {
        Console.WriteLine($"This is a cube with side length: {Side}.");
    }
}

In this structure, the Cube class inherits from Shape. By using the virtual keyword in the base class and the override keyword in the derived class, we achieve runtime polymorphism. This ensures that the correct method is called based on the actual object type, even if it is referenced through a base class pointer.

Tags: C# .NET OOP Operator Overloading Inheritance

Posted on Thu, 04 Jun 2026 18:37:22 +0000 by fesan