You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
System.Math is soft deprecated and we aren't exposing new APIs on it, so the alternative design is a non-starter.
In addition, it would be confusing when placed next to the existing Max/Min APIs which are the arithmetical comparisons and not the IComparer based comparisons
Essentially you have:
(TComparer.Compare(x, y) > 0) ? x
Follows the IComparer rules rules
Is suitable for use in sorting algorithms, hashsets, etc
INumber<T>.Max(x, y)
Follows the arithmetic rules of the T
May not be suitable for sorting algorithms, hashsets, etc
Enumerable.Max exposes the former, defaulting to Comparer<T>.Default; while INumber<T>.Max defaults to the latter, which also means that TensorPrimitives.Max<T>(ReadOnlySpan<T>) where T : INumber<T> does as well
I'd think if we exposed support here, it'd need to match Enumerable behavior and then also generally match the overloads that we expose on other MemoryExtensions APIs:
IComparable<T> would would probably require boxing the values, and Enumerable.Min/Max does not have any such constraints; It relies on Comparer<T>.Default.
Right, in general you can use EqualityComparer<T>.Default or Comparer<T>.Default instead of constraining yourself to be where T : IEquatable<T> or where T : IComparer<T>
We've often explicitly removed constraints to allow support for more types and because the pattern is well optimized by the JIT.
Is the behaviour when given an empty sequence going to be the same as in Enumerable? That is, return null if T is a reference type or a nullable value type (i.e. default(T) is null), but throw InvalidOperationException if T is a non-nullable value type.
That would be my default here. We could alternatively expose it as IndexOfMax and simply return -1 on no match, something we couldn't trivially do with Enumerable.
Let's leave the TComparer overloads out until we see a comprehensive need (or a language feature to benefit from it, like functional interfaces)
Should follow up to see what other Enumerable members should be copied over
Since these are emulating Enumerable.Min/Max, they should behave the same... so if T is a value type empty throws, if it is a reference type it returns null.
The IComparer<T> parameters should have been nullable to ensure consistency with the other signatures (both in LINQ, MemoryExtensions, and throughout .NET).
Activity
dotnet-policy-service commentedon Apr 17, 2026
Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.
ladeak commentedon Apr 17, 2026
You would probably also need an
IComparer<T>parameter and orIComparable<T>generic constraint.tannergooding commentedon Apr 17, 2026
System.Mathis soft deprecated and we aren't exposing new APIs on it, so the alternative design is a non-starter.In addition, it would be confusing when placed next to the existing
Max/MinAPIs which are the arithmetical comparisons and not theIComparerbased comparisonsEssentially you have:
(TComparer.Compare(x, y) > 0) ? xIComparerrules rulesINumber<T>.Max(x, y)TEnumerable.Maxexposes the former, defaulting toComparer<T>.Default; whileINumber<T>.Maxdefaults to the latter, which also means thatTensorPrimitives.Max<T>(ReadOnlySpan<T>) where T : INumber<T>does as wellI'd think if we exposed support here, it'd need to match
Enumerablebehavior and then also generally match the overloads that we expose on other MemoryExtensions APIs:We do not need the
Span<T>overloads, particularly thanks to theFirst Class Spansfeature.tannergooding commentedon Apr 17, 2026
I'd be fine taking this to API review if the proposal was updated to what I suggested, covering the 6 overloads.
MizardX commentedon Apr 17, 2026
I agree. Updated.
ladeak commentedon Apr 17, 2026
Would you need a generic constraint also for:
public T? Min/Max<T>(ReadOnlySpan<T> span);?MizardX commentedon Apr 17, 2026
I don't think so.
IComparable<T>would would probably require boxing the values, andEnumerable.Min/Maxdoes not have any such constraints; It relies onComparer<T>.Default.tannergooding commentedon Apr 17, 2026
Right, in general you can use
EqualityComparer<T>.DefaultorComparer<T>.Defaultinstead of constraining yourself to bewhere T : IEquatable<T>orwhere T : IComparer<T>We've often explicitly removed constraints to allow support for more types and because the pattern is well optimized by the JIT.
jkotas commentedon Apr 17, 2026
Should this be
public T? Min<T>(this ReadOnlySpan<T> span);so that it is actually an extension method?tannergooding commentedon Apr 17, 2026
Yes, it likely should. There's a couple APIs on
MemoryExtensionsandEnumerablethat aren't extensions and its very easy to forget they exist.MizardX commentedon Apr 17, 2026
Good catch
KalleOlaviNiemitalo commentedon Apr 17, 2026
Is the behaviour when given an empty sequence going to be the same as in Enumerable? That is, return null if T is a reference type or a nullable value type (i.e.
default(T) is null), but throw InvalidOperationException if T is a non-nullable value type.tannergooding commentedon Apr 17, 2026
That would be my default here. We could alternatively expose it as
IndexOfMaxand simply return-1on no match, something we couldn't trivially do withEnumerable.bartonjs commentedon May 5, 2026
Video
tannergooding commentedon May 19, 2026
The
IComparer<T>parameters should have been nullable to ensure consistency with the other signatures (both in LINQ, MemoryExtensions, and throughout .NET).