Skip to content

C# Feature - Generic math

Generic math uses static abstract members in interfaces so algorithms can work across numeric types without boxing or duplicated overloads.

C# 11.0 (2022)

int Sum(int left, int right) => left + right;
double Sum(double left, double right) => left + right;
static T Sum<T>(T left, T right) where T : System.Numerics.INumber<T>
=> left + right;
  • Generic math is most useful for reusable numeric libraries.
  • Constrain with the narrowest numeric interface that fits the algorithm.