I recently got my hands on a copy of C# 7.0 in a Nutshell by Joseph and Ben Albahari. As I was browsing through the chapter on advanced C#, page 199 in particular where it begins covering operator overloading; I began wondering, has there been any official word on operator overloading similar to extension methods for at least primitive types? For example:
// Traditional Overload
public static MyType operator +(MyType, MyType);
// Traditional Extension Method
public static int Sum(this int, int[]);
// Possible Combination?
public static int operator +(this int, int, string);
In the example above, there is traditional overloading of the addition operator; this allows me to perform custom addition of properties of my type in order to supply a new value. Then there is the traditional extension method, in which we have the ability to extend types by adding methods we deem useful; similar to the example above, if I am frequently performing addition of all the values in an integer array to get their sum, then I could create a useful extension method to implement:
int sum = int.Sum(myArrayOfIntegers);
With all of this said, extended operator overloading could be useful, at least for primitives. For example:
public static int operator +(this int i, int x, string y) {
// Perform parsing of the string and return the newly added value between x and y.
}
I could then perform arithmetic on multiple primitive types where useful and I wouldn't have to constantly attempt to parse data in my code. It's not that parsing data is difficult, but when you have to do the same thing hundreds of times (including calling a method to perform the parse and arithmetic), it can become tedious if anything.
I have done some searching and can't find anything relating to this topic since this post which was answered in 2008. Ten years is a long time, and I would hope the opinion from that post has changed since then.
I am sorry to report that we will not be doing this in the next release. We did take extension members very seriously in our plans, and spent a lot of effort trying to get them right, but in the end we couldn't get it smooth enough, and decided to give way to other interesting features.
This is still on our radar for future releases. What will help is if we get a good amount of compelling scenarios that can help drive the right design.
Still on our radar isn't very promising in my opinion.
Please only supply an answer if the resource is credible and current (or within the previous 2 years).