Search code examples
c#typesoperator-overloadingcoercion

Overloading Operators for Numeric Types in C#


I'm creating a numeric type called BigNum which is meant to be a really expandable BCD. In the process, I'm overloading most/all of the normal numeric operators. My main question is, do I need to overload each operator for each built-in numeric type (long, ulong, int, uint, short, ushort, byte, sbyte, float, double, decimal), or can I just overload it for double/decimal and have the system coerce the other types behind the scenes?

Edit: I'm also wondering if having the system coerce to double (if indeed this is viable) will maintain accuracy with whole numbers.


Solution

  • You should provide overloads for all the types you intend to support to avoid unnecessary conversions.