5

Being new to C#, I am aware of the purpose and uses of operator overloading, and I can see how it's sometimes elegant in very specific cases such as negating a value object to negate all of its fields at once, however such things can always be done with simple, speaking methods. Is there some standard I can use as a guideline, when or whether to overload operators or when to simply use a method? I couldn't find anything like that, so is it just a matter of taste/programming paradigm/style?

Andreas Hartmann
  • 1,825
  • 4
  • 23
  • 36
  • 1
    I've never heard of any guideline describing how to choose between operator and a method. But working with C# and .Net for a few years I rarely saw operators being overloaded (except == and !=). – Sergey.quixoticaxis.Ivanov Apr 21 '17 at 17:44
  • 1
    I would certainly do it if every other solution to my problem (that I could think of) were ridiculous. Never say never, but in ten years of writing C# professionally, I can't recall such a case. – 15ee8f99-57ff-4f92-890c-b56153 Apr 21 '17 at 17:44
  • So it does sound like a matter of taste? Coming from Java, I didn't have the possibility in the past and I'm hesitant to make use of it. – Andreas Hartmann Apr 21 '17 at 17:47
  • 2
    I think this is mostly a personal preference thing. Our C# code base has some core geometric types (vector, point, direction) that have overloaded operators to support cross/dot products, transformations, etc. I think it makes sense for fundamental data types like this, but would avoid it in most cases. – E. Moffat Apr 21 '17 at 17:51
  • 3
    If you're writing arithmetic primitives (like rational, complex, or vector/matrix types) forcing users to use static methods to describe simple transformations very quickly becomes unwieldy. `p0 = r * u - Axis + origin;` is a lot more readable than `p0 = Point.Add( Vector.Subtract( Vector.Multiply( r, u ), Axis ), origin );`. The only time I've used operator overloading outside of an algebraic context is when writing a parser generator where I overloaded `|` to represent alternatives (and `&` to represent sequences). – Kyle Apr 21 '17 at 17:58
  • 1
    I'd say if you're looking for a guideline it's this: if you expect users would want to form complicated expressions and there's a well-established notation for those expressions, then overloading operators might be appropriate. – Kyle Apr 21 '17 at 18:01
  • I think, overloading operators is only usefull when defining a new type that can do some arithmetic calculations or 1 on 1 type conversion. – Jeroen van Langen Apr 21 '17 at 18:10
  • It is a boon to library writers. The .NET Framework is a good example, adding a TimeSpan to a DateTime or multiplying two Decimals feels entirely natural. But require operator overloads, the runtime doesn't know anything about these types. Which is the guidance, only use them when it feels "natural" and you can afford to document it well. – Hans Passant Apr 21 '17 at 18:53

2 Answers2

4

This is the closest thing I have found to operator overloading guides of any sort: MSDN operator overload

I came to C# from a C++ background where operator overloads were part of the norm. The problem with C# that makes it less useful is that all objects are references. Because of this you cannot overload the = operator which, in my mind would be the most useful operator to overload for complex objects. That being said, writing an extension method can achieve the same goal with the same code.

And as noted in the comments, I have also been working in C# for nearly 10 years and have yet to see someone overload an operator.

Chris Bartlett
  • 312
  • 1
  • 11
  • +1 same here `And as noted in the comments, I have also been working in C# for nearly 10 years and have yet to see someone overload an operator` – Jaydeep Karena Nov 14 '18 at 11:12
  • 1
    @jaydeepkarena The BigInteger class in the System.Numerics namespace use operator overloading so that you can mix BigInteger instances with Int32, UInt32 and the other integral structs with the binary arithmetic operators. In Java, you have no operator overloading and therefore you resort to using the add(), divide() etc... members of BigInteger and BigDecimal classes for the same results. Also, I had problems with integrals being non-reference types so I made my own ManagedIntegral classes wrapping the unmamanged integrals which make extensive use of operator overloading. – Patrik Nusszer Nov 21 '19 at 07:03
2

The cases where I want to use operator overloading are the ones where C# won't allow its use - in extension methods. It would certainly be nice to overload TimeSpan with operator/ and perhaps operator* but you can't have extension operators.

Otherwise I think it only makes sense when creating algebraic types or (perhaps) a new tiny language that uses chained operators (ala C++ output operators). E.g. I could see a variation of Linq based around operators being interesting.

NetMage
  • 26,163
  • 3
  • 34
  • 55