dotnet / dotnet/dotnet-api-docs

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

Aperta
#7,922 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area-System.Linq Pri3
Lingua principale
C#
Stelle
950
Fork
1.7k
Merge medio
2g 19h
PR unite (30g)
52

Descrizione

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).

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia dalla documentazione dell'API di Enumerable.Min() e Enumerable.Max() e confronta la formulazione relativa a null con il comportamento descritto nell'issue. Esamina i percorsi referenziati Min.cs e Max.cs per comprendere il comportamento delle sequenze vuote, quindi aggiorna la documentazione per trattare i tipi valore nullable e il comportamento di generazione di un'eccezione per le sequenze vuote con i tipi non-reference.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.