dotnet / dotnet/dotnet-api-docs

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

Open
#7,922 3 comments 0 reactions 0 assignees View on GitHub
area-System.Linq Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

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:

```csharp
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`) 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)`](https://github.com/dotnet/runtime/blob/e3ecc8372630f22011815d64099598e30bcb43a7/src/libraries/System.Linq/src/System/Linq/Min.cs#L598) and [the same behavior in .Max()`](https://github.com/dotnet/runtime/blob/e3ecc8372630f22011815d64099598e30bcb43a7/src/libraries/System.Linq/src/System/Linq/Max.cs#L646) 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).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.