Home
Map
operator KeywordUse the operator keyword to develop overloaded binary and unary operators.
C#
This page was last reviewed on Nov 15, 2023.
Operator. Consider a C# class instance. You cannot use a plus sign to add 2 classes together. But sometimes this makes logical sense—it should be possible.
With overloaded operators, we can overload the plus sign (and other symbols). Overloaded operators sometimes improve program syntax.
Detail With the operator keyword, public static operator methods used by the compiler when the designated operators are encountered.
This program declares a Widget class. Here, Widgets can be added together or incremented. In the Widget class, we provide 2 public static methods: operator +, and operator ++.
class
Return The methods return an instance of Widget. They receive 2 or 1 parameters—for binary (+) or unary (++).
Detail Here a new Widget instance is created and it uses the increment ++ operator. Its value is increased by 1, two times.
Increment
Next Another Widget is created. And finally we add the 2 widgets together with a single "+" operator.
So When we add the 2 Widgets together, a new Widget is returned. This is conceptually the same way the string type works.
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); } }
1 2 1 3
Operator list. Many but not all operators in the C# language can be overloaded. This comes from the C# specification, which has more in-depth information on overloading.
+ - ! ~ ++ -- true false
+ - * / % & | ^ << (shift left) >> (shift right) == != > (greater than) < (less than) >= <=
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 commonly used.
Detail In the .NET Framework itself, the string type has overloads and these are useful (this is how concatenation works).
string.Concat
Thus If you have a type that will be used in many places, then overloading it could be a good idea.
A summary. The operator keyword is used for overloading binary and unary operators. We provided an example of operator overloading. We saw a list of all the overloadable C# operators.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 15, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.