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#:
Explanation
- Operator overloading function is marked with static so that it can be accessed without creating the object.
- The operator is returning the instance of
CommaStr
and the function also defined inside the class CommaStr. - The keyword ‘
operator
’ says that we are overloading an operator and the sign+
says, we are overloading the + operator. - 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.
Explanation
- 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. - In the class, we declared an operator overloading function and its parts were explained on the previous section.
- 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.

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:
Explanation
- Here we construct three instances of the CommaStr object. At this stage, the internal property data holds nothing.
- From the text boxes, we get the user entered value and populate the ‘
data
’ property of each instance. - 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 operandstr3
. We store the operator’s return value into a variableResult
of type CommaStr. - 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:

6. Code Reference
6.1 CommaStr.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OverloadingOpr { public class CommaStr { //Sample 01: Store the String as Property public string data { get; set; } //Sample 02: Overload the + Operator public static CommaStr operator + (CommaStr S1, CommaStr S2) { CommaStr NewStr = new CommaStr(); NewStr.data = (S1.data + "," + S2.data); return NewStr; } } } |
6.2 Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace OverloadingOpr { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void cmdComma_Click(object sender, EventArgs e) { //Sample 03: Create Three Instances of CommaStr CommaStr str1 = new CommaStr(); CommaStr str2 = new CommaStr(); CommaStr str3 = new CommaStr(); str1.data = txtStr1.Text; str2.data = txtStr2.Text; str3.data = txtStr3.Text; //Sample 04: Use Operator Overloading CommaStr Result = str1 + str2 + str3; txtResult.Text = Result.data; } } } |
Categories: C#
Tags: Operator Overloading