59

Say I want to have a method that takes any kind of number, is there a base class (or some other concept) that I can use?

As far as I know I have to make overloads for all the different numeric types (Int32, Int16, Byte, UInt32, Double, Float, Decimal, etc). This seems awfully tedious. Either that or use the type object and throw exceptions if they are not convertible or assignable to a double - which is pretty bad as it means no compile time checking.

UPDATE: OK thanks for the comments, you are right Scarecrow and Marc, in fact declaring it as Double actually works for all except Decimal.

So the answer I was looking for is Double - it acts like a base class here since most numeric types are assignable to it. (I guess Decimal is not assignable to Double, as it could get too big.)

public void TestFormatDollars() {
    int i = 5;
    string str = FormatDollars(i);   // this is OK
    byte b = 5;
    str = FormatDollars(b);     // this is OK
    decimal d = 5;
    str = FormatDollars(d);     // this does not compile - decimal is not assignable to double
}

public static string FormatDollars(double num) {
    return "$" + num;
}
thatguy
  • 21,059
  • 6
  • 30
  • 40
mike nelson
  • 21,218
  • 14
  • 66
  • 75

7 Answers7

28

The answer is: you don't need to provide overloads for ALL the numeric types, just for Double and Decimal. All others (except maybe some very unusually large ones) will be automatically converted to these.

Not a base class but in fact that was the red herring. The base class System.ValueType doesn't help much as it includes types that are not numerics. The language reference i was reading was what got me confused in the first place :)

(I was just looking for who to attribute the answer to and it was a combination of Scarecrow and Marc Gravell, but since they were comments i have put the answer here)

mike nelson
  • 21,218
  • 14
  • 66
  • 75
15

There isn't one (or at least, not one that just means "numbers"). You could use:

void Foo<T>(T value) where T : struct {...}

But that allows any struct - not just numbers. If you want to do arithmetic, generic operators may be of use. Other than that; overloads it the most viable option.

thatguy
  • 21,059
  • 6
  • 30
  • 40
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
12

What I do:

 public interface INumeric<T>
 {
     T Zero { get; }
     T One { get; }
     T MaxValue { get; }
     T MinValue { get; }
     T Add(T a, T b);
     // T Substract(....
     // T Mult...
 }  

 public struct Numeric: 
     INumeric<int>, 
     INumeric<float>,
     INumeric<byte>,
     INumeric<decimal>,
     // INumeric<other types>
 {
     int INumeric<int>.Zero => 0;
     int INumeric<int>.One => 1;
     int INumeric<int>.MinValue => int.MinValue;
     int INumeric<int>.MaxValue => int.MaxValue;
     int INumeric<int>.Add(int x, int y) => x + y;

     // other implementations...
 }

Now, you can use it in a method:

bool IsZero<TNum, T>(TNum ops, T number) 
   where TNum : INumeric<T>
{
   return number == ops.Zero;      
}

or extension method

 public static bool IsZero<TNum, T>(this TNum ops, T number)
      where TNum : INumeric<T>
 {
      return number == ops.Zero;
 }

and in your code:

 ...
 var n = new Numeric(); // can be an static prop

 Console.WriteLine(IsZero(n, 5)); // false
 Console.WriteLine(IsZero(n, 0f)); // true
 Console.WriteLine(IsZero(n, "0")); // compiler error

or, with extension method:

 Console.WriteLine(n.IsZero(5));  // false
 Console.WriteLine(n.IsZero(0f)); // true
 Console.WriteLine(n.IsZero("0")); // compiler error
Luis
  • 436
  • 4
  • 11
  • 1) Obsolete because Microsoft has released new feature. .NET 7 features the INumber interface. 2) Your example of IsZero method scares me when you do Equality. How do you properly handle floats and doubles? – Eric Wood Apr 26 '23 at 20:08
9

The short answer is: Numeric types are value types, hence they derive from System.ValueType. The full answer is: you should read this article from MSDN. Moreover I think that you should read C# language reference :). Value type not equals numeric type, because values types include also structures and enumerations.

Robert Paulson
  • 17,603
  • 5
  • 34
  • 53
Dmitrii Lobanov
  • 4,897
  • 1
  • 33
  • 50
  • 1
    The article from MSDN does not really consider what is the best type to use when you want a parameter to catch all kinds of number and nothing else - which is what i was asking. – mike nelson May 06 '09 at 10:42
  • Yes, but the article shows that decimal type and other floating-point types are different kind of types. – Dmitrii Lobanov May 06 '09 at 12:01
7

The base class of the numeric types is ValueType.

Unfortunately that still won't help you: DateTime, bool, Enum and hundreds of other types also derive from ValueType. There's no NumericType base class in .NET.

LukeH
  • 263,068
  • 57
  • 365
  • 409
  • Value types aren't necessarily numbers though, are they? – Lucas Lindström May 06 '09 at 09:32
  • ValueType also includes strings and other non-numeric value types, however. – Noldorin May 06 '09 at 09:33
  • @Noldorin, You're right that ValueType includes hundreds of non-numeric types, however string isn't one of them. String is a reference type (it just behaves a little bit like a value type). – LukeH May 06 '09 at 09:45
  • Also i can't use ValueType as a parameter declaration and then call it with a number, it says it can't find the best match overload. – mike nelson May 06 '09 at 09:48
  • @mike, I'm not sure what you mean - I can declare a method "public void Foo(ValueType val)" and pass numbers to it, although I can't really do anything useful with them inside the method. – LukeH May 06 '09 at 10:07
  • @Luke: Yeah, not sure why I said string! String is an immutable reference type, although it indeed appears like a value type. – Noldorin May 06 '09 at 10:29
  • @Luke - yes you are right, sorry i got that wrong, thanks for pointing that out. I have now gathered that in fact Double and Decimal cover almost all cases, so i only need two overloads and not squillions as i had first thought. – mike nelson May 06 '09 at 10:34
6

In .NET 7 a new API for generic math was finally introduced.

.NET 7 introduces new math-related generic interfaces to the base class library. The availability of these interfaces means you can constrain a type parameter of a generic type or method to be "number-like".

It features the INumber<T> interface that is now implemented by all numeric types, so you can do this:

public static string FormatDollars<T>(T num) where T : INumber<T>
{
    return "$" + num;
}

It also provides fine-grained numeric interfaces to identify type categories, e.g.:

  • IBinaryInteger<T> for Byte (byte), Int16 (short), Int32 (int), Int64 (long), ...
  • IFloatingPoint<T> for Double (double), decimal (Decimal), ...
  • ...

For operators it offers a variety of operator interfaces that are implemented by all numeric types.

  • IAdditionOperators<TSelf,TOther,TResult> for plus (+).
  • IMultiplyOperators<TSelf,TOther,TResult> for multiply (*).
  • ...

I assume that your question was not explicitly about value types but numeric types. However, if you need generic type constraints that enforce value types as well, specify both.

public static string FormatDollars<T>(T num) where T : struct, INumber<T>
{
   return "$" + num;
}
thatguy
  • 21,059
  • 6
  • 30
  • 40
0

Are overloaded method signitures out of the question here? If you want a constrained group of methods to performe the same task you could voerload the public method and call a private method that takes any number via casting the input to a double.

Greg B
  • 14,597
  • 18
  • 87
  • 141