Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Operator Overloading in C#

1. Operator Overloading

We can overload an operator using the operator keyword followed by the symbol for the operator. In this example, we will overload an + operator to append comma with the strings. Since + is a binary operator, we need two instances of the string. In this example, we will write a class called CommaStr and have a static method with name ‘+’ which acts as an overloaded operator function. Unlike C++, Operator Overloading can be done for very specific operators in C#. You can refer MSDN for list of operators that can support overloading.

2. Declare Operator Overloading Function

The below picture shows how one can declare a operator ovarloading function C#:

Fig 1. C# Binary Operator Overloading Syntax
Fig 1. C# Binary Operator Overloading Syntax


  1. Operator overloading function is marked with static so that it can be accessed without creating the object.
  2. The operator is returning the instance of CommaStr and the function also defined inside the class CommaStr.
  3. The keyword ‘operator’ says that we are overloading an operator and the sign + says, we are overloading the + operator.
  4. Since + is a binary operator, it needs two operands, and we receive both the operands as parameters. Here, S1 is left side of the + and S2 is right side of the plus.

3. C# Class with Operator Overloading Function

The below code shows the Operator overloading function which overloads the + operator.

Fig 2. C# Class With Overloading + Operator
Fig 2. C# Class With Overloading + Operator


  1. The class CommaStr is exposing a single public property called data which stores and retrieves the string value. Note, since we do not want any validation, we simply have an empty get and set without any member variables.
  2. In the class, we declared an operator overloading function and its parts were explained on the previous section.
  3. In the body of the function, we use both the operands of type CommaStr and read the data property. The + overload appends a comma between the first and second operands’ data part. It also creates new instance of CommaStr, populates it with the comma appended text and returns it to the caller. Now, we can add two CommaStr instances using the + operator to append a comma in between.

4. Win Form to Test + Overloading

The below Dotnet Win form tests the overloaded + operator which added to the CommaStr class.

Fig 3. Windows Form To Test Operator Overloading
Fig 3. Windows Form To Test Operator Overloading

Here, we place three texts in the three text boxes and click on the push button ‘Combine by Comma’. In the click event, we append all three texts with a comma in between and show the resulting string in the text box located at the bottom. Let us see how we use the overloaded operator.

5. Using Overloaded + Operator

On the button click handler, we will make use the overloaded + operator. The code snippet is below:

Fig 4. Using the Overloaded C# + Operator
Fig 4. Using the Overloaded C# + Operator


  1. Here we construct three instances of the CommaStr object. At this stage, the internal property data holds nothing.
  2. From the text boxes, we get the user entered value and populate the ‘data’ property of each instance.
  3. Here, we are using the + operator on the CommaStr object. We know that the class overloads the + operator and in the body, it appends the comma between the operands. In our code snippet, the operator is called two times. First time with two operands str1 and str2. Second time with the return value (str1 + str2) and the operand str3. We store the operator’s return value into a variable Result of type CommaStr.
  4. In the text box, we print the output of the comma split string which is combined from all three text boxes.

Below picture shows the sequence of operations. The numbers denote the steps to perform the test:

Fig 5. Testing the Sample Windows Form
Fig 5. Testing the Sample Windows Form

6. Code Reference

6.1 CommaStr.cs

6.2 Form1.cs

Categories: C#


Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.