How C# 11 static abstract members in interfaces enable universal numeric algorithms:
public static T SumArray<T>(ReadOnlySpan<T> numbers) where T : INumber<T>
{
T total = T.Zero;
foreach (T n in numbers)
total += n;
return total;
}
// Works on int, float, double, decimal, Half, BigInteger, and custom types!
int sumInt = SumArray<int>([1, 2, 3, 4]);
double sumDouble = SumArray<double>([1.5, 2.5, 3.5]);The JIT compiler monomorphizes generic math calls into native CPU assembly with zero boxing and zero interface dispatch overhead!