C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
With the operator keyword, public static operator methods used by the compiler when the designated operators are encountered. This sometimes makes programs and frameworks clearer.
Example. This program declares a Widget class. Here, Widgets can be added together or incremented with the same syntax used for integers. In the Widget class, we provide two public static methods: operator +, and operator ++.
These two methods return an instance of Widget. They receive two or one formal parameters depending on whether the operator is binary (+) or unary (++). They are overloaded operator implementations.
C# program that uses operator keyword using System; class Widget { public int _value; public static Widget operator +(Widget a, Widget b) { // Add two Widgets together. // ... Add the two int values and return a new Widget. Widget widget = new Widget(); widget._value = a._value + b._value; return widget; } public static Widget operator ++(Widget w) { // Increment this widget. w._value++; return w; } } class Program { static void Main() { // Increment widget twice. Widget w = new Widget(); w++; Console.WriteLine(w._value); w++; Console.WriteLine(w._value); // Create another widget. Widget g = new Widget(); g++; Console.WriteLine(g._value); // Add two widgets. Widget t = w + g; Console.WriteLine(t._value); } } Output 1 2 1 3
Using Widget operators. Now look at the Main() entry point. A new Widget instance is created and it uses the increment ++ operator. Its value is increased by one. Then we increase it by one again.
Next: Another Widget is created. And finally we add the two widgets together with a single "+" operator.
So: When we add the two Widgets together, a new Widget is returned. This is conceptually the same way the string type works.
Operator list. Many but not all operators in the C# language can be overloaded. I borrowed this list from the C# specification, which has more in-depth information on overloading some of these operators.
Unary operators you can overload + - ! ~ ++ -- true false Binary operators you can overload + - * / % & | ^ << >> == != > < >= <=
Discussion. It is not necessary to overload operators on every class you create. My opinion is that overloading operators is rarely required. It helps only on types that are important and commonly used.
In the .NET Framework itself, the string type has overloads and these are useful. If you have a type that will be used in thousands of places, then overloading it could be a good idea if those overloads make intuitive sense.
Tip: The string type itself, key and fundamental to the C# language, uses overloaded operators. This is how concatenation works.
Summary. This example looked at the operator keyword in the C# language and its usage for overloading binary and unary operators. We provided a compile-ready example of operator overloading.
And: We reproduced, for easy reference, a list of all the overloadable operators in this language.