Skip to content

[API Proposal]: MemoryExtensions.Min/Max #127083

Closed

Description

@MizardX

Background and motivation

I want to take the minimum or maximum of three or more DateTimeOffset.

Enumerable.Min/Max requires heap allocation. Math.Min/Max only takes two numeric arguments. INumber.Min/Max only implemented for numeric types.

I want to get find the minimum or maximum of a fixed set of values without extra heap allocation.

API Proposal

namespace System;

public partial static class MemoryExtensions
{
    public T? Min<T>(this ReadOnlySpan<T> span);
    public T? Min<T>(this ReadOnlySpan<T> span, IComparer<T> comparer);
    public T? Min<T, TComparer>(this ReadOnlySpan<T> span, TComparer comparer) where TComparer : IComparer<T>;

    public T? Max<T>(this ReadOnlySpan<T> span);
    public T? Max<T>(this ReadOnlySpan<T> span, IComparer<T> comparer);
    public T? Max<T, TComparer>(this ReadOnlySpan<T> span, TComparer comparer) where TComparer : IComparer<T>;
}

Similar to Enumerable.Min/Max, using Comparer<T>.Default as comparer if not specified.

API Usage

Span<DateTimeOffset> values = [
    DateTimeOffset.Parse("2026-03-14 16:18:04 +01:00"),
    DateTimeOffset.Parse("2026-04-16 00:50:35 +01:00"),
    DateTimeOffset.Parse("2026-03-23 18:13:43 +01:00")
];
Console.WriteLine($"Max: {values.Max()}");

Alternative Designs

Expand Math.Min/Max to both generic and variadic arguments.

namespace System;

public partial static class Math
{
    public T? Min<T>(params ReadOnlySpan<T> span);
    public T? Max<T>(params ReadOnlySpan<T> span);
}

Risks

It might cause ambiguity with Enumerable.Min/Max

Activity

  1. added
    api-suggestion
    on Apr 17, 2026
  2. added
    untriaged
    on Apr 17, 2026
  3. dotnet-policy-service commented on Apr 17, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-memory
    See info in area-owners.md if you want to be subscribed.

  4. ladeak commented on Apr 17, 2026

    @ladeak
    Contributor

    You would probably also need an IComparer<T> parameter and or IComparable<T> generic constraint.

  5. tannergooding commented on Apr 17, 2026

    @tannergooding
    Member

    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:

    namespace System;
    
    public partial static class MemoryExtensions
    {
        public T? Min<T>(ReadOnlySpan<T> span);
        public T? Min<T>(ReadOnlySpan<T> span, IComparer<T> comparer);
        public T? Min<T, TComparer>(ReadOnlySpan<T> span, TComparer comparer) where TComparer : IComparer<T>;
    
        public T? Max<T>(ReadOnlySpan<T> span);
        public T? Max<T>(ReadOnlySpan<T> span, IComparer<T> comparer);
        public T? Max<T, TComparer>(ReadOnlySpan<T> span, TComparer comparer) where TComparer : IComparer<T>;
    }

    We do not need the Span<T> overloads, particularly thanks to the First Class Spans feature.

  6. tannergooding commented on Apr 17, 2026

    @tannergooding
    Member

    I'd be fine taking this to API review if the proposal was updated to what I suggested, covering the 6 overloads.

  7. MizardX commented on Apr 17, 2026

    @MizardX
    Author

    I agree. Updated.

  8. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Apr 17, 2026
  9. ladeak commented on Apr 17, 2026

    @ladeak
    Contributor

    Would you need a generic constraint also for: public T? Min/Max<T>(ReadOnlySpan<T> span); ?

  10. MizardX commented on Apr 17, 2026

    @MizardX
    Author

    I don't think so.

    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.

  11. tannergooding commented on Apr 17, 2026

    @tannergooding
    Member

    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.

  12. jkotas commented on Apr 17, 2026

    @jkotas
    Member

    public T? Min(ReadOnlySpan span);

    Should this be public T? Min<T>(this ReadOnlySpan<T> span); so that it is actually an extension method?

  13. tannergooding commented on Apr 17, 2026

    @tannergooding
    Member

    Yes, it likely should. There's a couple APIs on MemoryExtensions and Enumerable that aren't extensions and its very easy to forget they exist.

  14. MizardX commented on Apr 17, 2026

    @MizardX
    Author

    public T? Min(ReadOnlySpan span);

    Should this be public T? Min<T>(this ReadOnlySpan<T> span); so that it is actually an extension method?

    Good catch

  15. added this to the 11.0.0 milestone on Apr 17, 2026
  16. KalleOlaviNiemitalo commented on Apr 17, 2026

    @KalleOlaviNiemitalo

    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.

  17. tannergooding commented on Apr 17, 2026

    @tannergooding
    Member

    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.

  18. bartonjs commented on May 5, 2026

    @bartonjs
    Member

    Video

    • 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.
    namespace System;
    
    public partial static class MemoryExtensions
    {
        public T? Min<T>(this ReadOnlySpan<T> span);
        public T? Min<T>(this ReadOnlySpan<T> span, IComparer<T> comparer);
    
        public T? Max<T>(this ReadOnlySpan<T> span);
        public T? Max<T>(this ReadOnlySpan<T> span, IComparer<T> comparer);
    }
  19. added
    api-approved
    and removed
    api-ready-for-review
    on May 5, 2026
  20. tannergooding commented on May 19, 2026

    @tannergooding
    Member

    The IComparer<T> parameters should have been nullable to ensure consistency with the other signatures (both in LINQ, MemoryExtensions, and throughout .NET).

  21. locked and limited conversation to collaborators on Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Memory

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions