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
Background and motivation
I want to take the minimum or maximum of three or more
DateTimeOffset.Enumerable.Min/Maxrequires heap allocation.Math.Min/Maxonly takes two numeric arguments.INumber.Min/Maxonly 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
Similar to
Enumerable.Min/Max, usingComparer<T>.Defaultas comparer if not specified.API Usage
Alternative Designs
Expand
Math.Min/Maxto both generic and variadic arguments.Risks
It might cause ambiguity with
Enumerable.Min/Max