Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enumerable.Min<TSource>() documentation should mention null filtering behavior #7923

Open
Labels
Pri3Indicates issues/PRs that are low priorityarea-System.Linqdoc-bugProblem with the content; needs to be fixed
@binki

Description

@binki

In regards tonull, the documentation states this:

IfTSource is a reference type and the source sequence is empty or contains only values that arenull, this method returnsnull.

It also states:

If typeTSource 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityarea-System.Linqdoc-bugProblem with the content; needs to be fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp