- Notifications
You must be signed in to change notification settings - Fork1.6k
Description
In regards tonull
, the documentation states this:
If
TSource
is a reference type and the source sequence is empty or contains only values that arenull
, this method returnsnull
.
It also states:
If type
TSource
implementsIComparable, theMax(IEnumerable) method uses that implementation to compare values. Otherwise, if typeTSource
implementsIComparable, that implementation is used to compare values.
This leads me to believe that the minimum value will be calculated purely based on the output ofIComparable<T>.Compare(T, T)
. That is, the output of.Min()
would be the same as.OrderBy(x => x).First()
and the output of.Max()
would be the same as.OrderBy(x => x).Last()
. Or that ifComparer<T>.Default.Compare(a, b) < 0
, then the output ofnew[] { a, b, }.Min()
would bea
. However, I found the following behavior instead:
Console.WriteLine($"Comparer<string>.Default.Compare(null,\"a\") =={Comparer<string>.Default.Compare(null,"a")}");Console.WriteLine($"new[]{{ null,\"a\", }}.OrderBy(x => x).First() =={new[]{null,"a",}.OrderBy(x=>x).First()}");Console.WriteLine($"new[]{{ null,\"a\", }}.Min() =={new[]{null,"a",}.Min()}");
with output:
Comparer<string>.Default.Compare(null, "a") == -1new[]{ null, "a", }.OrderBy(x => x).First() ==new[]{ null, "a", }.Min() == a
By experiencing this and examiningthesource, I know that.Min()
and.Max()
implicitly filter out any values equal tonull
as the first step (including nullable value types which box tonull
). However, this is not obvious from the documentation.
May you please alter the documentation to document this behavior?
Thanks!