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)
Before
Section titled “Before”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.