问题
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 "+"?
回答1:
You will have to resort to using overloading. A little like the Math
class is doing with function like Math.Max
where it support all numeric types.
The link proposed by CD is also resourceful.
回答2:
See this SO question for similar discussion.
Also, you can do this (with some tedious effort) if you're willing to create separate Types for each of the .Net core number types you want to use it for...
Create two structs, called say, MyInt, and MyDecimal which act as facades to the CTS Int32, and Decimal core types (They contain an internal field of that respective type.) Each should have a ctor that takes an instance of the Core CTS type as input parameter..
Make each one implement an empty interface called INumeric
Then, in your generic methods, make the constraint based upon this interface. Downside, everywhere you want to use these methods you have to construct an instance of the appropriate custom type instead of the Core CTS type, and pass the custom type to the method.
NOTE: coding the custom structs to properly emulate all the behavior of the core CTS types is the tedious part... You have to implement several built-in CLR interfaces (IComparable, etc.) and overload all the arithmetic, and boolean operators...
回答3:
Unfortunately, no such class/interface exists.
回答4:
int and the other numeric types are ValueTypes. As already said, all you can do is saying where T: struct
. Well, you could use reflection to check if the type argument implements addition... but i'm not sure if that's a good idea.
回答5:
The best you can do is where T : struct
which requires a ValueType; all numerics are ValueTypes.
Unfortunately, so are all user-defined structs, so that's the drawback. There's no specific generic that will accept all numeric classes.
I hesitate to even suggest this, but you might consider runtime checks against the type of T
in the method and throw if T
is not an int, long, short, float, or double. Unfortunately, this will not help a programmer at compile-time.
回答6:
This is a very common question; if you are using .NET 3.5, there is a lot of support for this in MiscUtil, via the Operator class, which supports inbuilt types and any custom types with operators (including "lifted" operators); in particular, this allows use with generics, for example:
public static T Sum<T>(this IEnumerable<T> source) {
T sum = Operator<T>.Zero;
foreach (T value in source) {
if (value != null) {
sum = Operator.Add(sum, value);
}
}
return sum;
}
Or for another example; Complex<T>
来源:https://stackoverflow.com/questions/1375026/c-sharp-numeric-base-class