Operator overloading or ad-hoc polymorphism lets you work with user defined types much the same way you work with fundamental data types Polymorphism is one of the basic principles of OOP (Object Oriented Programming). The word Polymorphism is derived from two words — “poly” meaning many, and “morph” meaning forms. Hence, polymorphism implies the existence of the same thing but in different forms. Operator overloading is an example of static polymorphism. You can leverage operator overloading or to add functionality to operators so as to work with user defined types much the same way you work with fundamental data types. What are the operators and what are the operators that can be overloaded? In an expression you typically have operators and operands. Operators are those that work on operands and they all are part of an expression. As an example, the following is an expression that contains two operands and one operator. The operands are X and Y and the operator is +. X + Y Operators are classified as unary, binary, comparison, assignment, etc. depending on their purpose and the number of operands on which the operators would work. Although most of the operators can be overloaded, certain restrictions apply. In essence, not all operators can be overloaded. The unary operators i.e., operators that work with one operand can be overloaded. You can also overload binary operators and comparison operators like, ==, !=, , =. However, you cannot overload the =, ?, ->, new, is, sizeof or typeof operators. You cannot also overload the conditional operators like && and || easily. Also, the array indexing operator [] cannot be overloaded. You can learn more on what all operators can be overloaded from this MSDN article. Implementing operator overloading in C# In this section we will explore how we can implement operator overloading in C#. Consider the following class named DistanceCalculator. public class DistanceCalculator { Int32 feet = 0, inch = 0; public DistanceCalculator(Int32 feet = 0, Int32 inch = 0) { this.feet = feet; this.inch = inch; } } Refer to the code listing above. The DistanceCalculator class contains two data members of type Int32, namely, feet and inch. We would now add more methods to this class to illustrate how operators can be overloaded. When working with operator overloading, you need to keep certain points in mind. The operator to be overloaded should have a corresponding method that is marked with the keyword operator. The arguments to the operator function are operands and your operator function can return a value. The operator function should be static and it should be a member of the containing type. The following code snippet illustrates how a typical operator function would look like. This example shows how the == and != operators can be overloaded. public static bool operator ==(DistanceCalculator obj1, DistanceCalculator obj2) { return obj1.Value == obj2.Value; } public static bool operator !=(DistanceCalculator obj1, DistanceCalculator obj2) { return obj1.Value != obj2.Value; } Note that Value is a property that returns the value in terms of inch. public Int32 Value { get { return (feet * 12) + inch; } } Here’s the complete code listing of the DistanceCalculator class. public class DistanceCalculator { Int32 feet = 0, inch = 0; public DistanceCalculator(Int32 feet = 0, Int32 inch = 0) { this.feet = feet; this.inch = inch; } public static bool operator ==(DistanceCalculator obj1, DistanceCalculator obj2) { return obj1.Value == obj2.Value; } public static bool operator !=(DistanceCalculator obj1, DistanceCalculator obj2) { return obj1.Value != obj2.Value; } public override bool Equals(object obj) { DistanceCalculator distanceCalculator = obj as DistanceCalculator; if (distanceCalculator != null) { return (distanceCalculator == this); } return false; } public override int GetHashCode() { return Value.GetHashCode(); } public Int32 Feet { get { return feet; } } public Int32 Inch { get { return inch; } } public Int32 Value { get { return (feet * 12) + inch; } } } The following code snippet illustrates how you can use the DistanceCalculator class. static void Main(string[] args) { DistanceCalculator obj1 = new DistanceCalculator(1, 2); DistanceCalculator obj2 = new DistanceCalculator(1, 2); Console.WriteLine((obj1 == obj2).ToString()); Console.Read(); } The following code snippet shows how you can overload the + operator to add two objects. public static DistanceCalculator operator +(DistanceCalculator obj1, DistanceCalculator obj2) { Int32 totalInches = obj1.Value + obj2.Value; Int32 feet = totalInches / 12; Int32 inch = totalInches % 12; DistanceCalculator temp = new DistanceCalculator(feet, inch); return temp; } Let’s add the following two properties to the DistanceCalculator class. The following code snippet shows how you can add two objects of type DistanceCalculator and return the resultant object as the same type, i.e., the returned object is of type DistanceCalculator. public Int32 Feet { get { return feet; } } public Int32 Inch { get { return inch; } } Here’s how you can create two instances of the DistanceCalculator class and take advantage of the overloaded operator function to assign the result in another object of the same type. static void Main(string[] args) { DistanceCalculator obj1 = new DistanceCalculator(1, 11); DistanceCalculator obj2 = new DistanceCalculator(1, 2); DistanceCalculator obj3 = obj1 + obj2; Console.WriteLine("Feet: "+obj3.Feet.ToString()); Console.WriteLine("Inch: " + obj3.Inch.ToString()); Console.Read(); } Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos