C# Feature - Generic math
Overview
Section titled βOverviewβGeneric math uses static abstract members in interfaces so algorithms can work across numeric types without boxing or duplicated overloads.
Introduced In
Section titled βIntroduced Inβ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;Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Generic math is most useful for reusable numeric libraries.
- Constrain with the narrowest numeric interface that fits the algorithm.