I want to write a C# method that can accept any number. Something like:
public static T Sum(T a, T b) where T : number { // (not real code)
return a + b;
}
But I don't see a "number" base class in C#, as exists in most other languages I've used. The numeric value types are IComparable, IFormattable, IConvertible, IComparable, and IEquatable, but nothing that seems to have any arithmetic capabilities. They're all structs, with no apparent common superclass, apart from object. (Forgive me if I'm screwing up the meaning here, since I'm not too familiar with C# structs and precisely all the ways they are like or unlike classes.)
Am I missing something, or is it not possible to write a method in C# that does "a + b" without declaring exactly what a and b are in the context of the "+"?