In many scenarios, operator overloads allow you to express yourself better. Why are they hardly used in C#?
They are not used very frequently because often it doesn't make sense to have mathematical operations (which the majority of operators are) over most classes of objects. The semantics of the operations you want are usually different in some way. You wouldn't use addition against a bunch of OrderLine objects, for example, when you actually want aggregation or grouping.
To use the example that you have now provided in the comments for your question, "+" may seem like a decent way to put child elements into a parent element but the operations have entirely different semantics. "+" suggests that you would be adding them together mathematically, when in fact you are aiming for a hierarchial relationship between the two. It may make sense to you, but I imagine that on first read it is probably not obvious to a lot of programmers.
They're rarely used because they rarely fit.
Another reason for their rarity is that while function overloading can be made self documenting, via the name of the function, and quite often by context, an operator is generally only one or two characters long.
Because of this, it can't be made explicitly clear what the overload is precisely intended to achieve. For example a string != comparison operator overload has no space within it to say whether it is comparing length, doing a character by character case insensitive comparison, or looking up the values in a static indexed array, and comparing them (although this is a ludicrous example).
I see them used more in languages making easy to do DSLs (domain specific languages).
A good example is PEG, which uses notation close of BNF: here, using operators is quite natural and expressive.
Lpeg library for Lua and Nimrod's PEG module are both using this capability, among others I suppose.
Another big issue about operator overloading in .Net is that you're not able to overload all operators. So e.g. the operators <<
or >>
are note available. And also a few more which are in some C++ classes heavily used are not able to be overloaded.
So in most scenarios it doesn't make sense, cause you don't have access to all operators unfortunately.