Advertisement
Advanced C#, Generics

What problem do generic math / static abstract interface members solve?

Short answer

They let you write algorithms over any numeric type by constraining to interfaces like INumber with static abstract operators, removing duplicated overloads.

Save C# mock interview
Advertisement
AModel answer
They let you write algorithms over any numeric type by constraining to interfaces like INumber<T> with static abstract operators, removing duplicated overloads.
T Sum<T>(IEnumerable<T> xs) where T : INumber<T> {
    T total = T.Zero; foreach (var x in xs) total += x; return total; }
Real-world example
One Sum method that works for int, double and decimal alike.