Skip to content

Enumerable.Min<TSource>() and Enumerable.Max<TSource>() wording regarding null for reference types is misleading #7922

Open
@binki

Description

@binki

In regards to null, the documentation states this:

If TSource is a reference type and the source sequence is empty or contains only values that are null, this method returns null.

However, I found the following behavior:

Console.WriteLine($"new[]{{ new int?(), }}.Min() == new int?(): {new[] { new int?(), }.Min() == new int?()}");
Console.WriteLine($"new int?[]{{ }}.Min() == new int?(): {new[] { new int?(), }.Min() == new int?()}");

has output:

new[]{ new int?(), }.Min() == new int?(): True
new int?[]{ }.Min() == new int?(): True

int? (Nullable<int>) is a value type, not a reference type. It is true that (object)new int?() == null is true, so it triggers the behavior of .Min() which returns default(TSource) and the same behavior in .Max()` instead of throwing. However, from reading the documentation, I would expect the throw-on-empty behavior.

Please clarify the documentation (which seems to also omit any mention of the throw-on-empty behavior for non-reference types).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityarea-System.Linq

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions