dotnet / dotnet/dotnet-api-docs

Is the enumeration order of the Stack<T> collection well defined?

Aperta
#8,083 5 commenti 1 reazione 0 assegnatari Vedi su GitHub
area-System.Collections help wanted Pri3
Lingua principale
C#
Stelle
949
Fork
1.7k
Merge medio
2g 19h
PR unite (30g)
52

Descrizione

Hi! I used the [`Stack`](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1) collection recently to implement an algorithm, whose correctness now depends on the `Stack` having a LIFO enumeration order. I checked the documentation to see if this enumeration order is well defined, but I can't find an explicit statement about this. I found though an [old StackOverflow question](https://stackoverflow.com/questions/37725449/stack-and-queue-enumeration-order "Stack and Queue enumeration order"), where [the accepted answer](https://stackoverflow.com/a/37725773/11178549) (by Matthew Watson) includes this excerpt:

> **The Stack.GetEnumerator() *strongly implies* that LIFO order is used.**
>
> If you look at [the example for `Stack.GetEnumerator()` in Microsoft's documentation](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1.getenumerator) and inspect the stated output, you can see that it is in LIFO order.
>
> This strongly suggests that Microsoft fully intend a Stack to be enumerated in LIFO order - but they forgot (or didn't bother to) to explicitly document this!

I would like to ask if the current LIFO behavior is just an implementation detail, or if it's a publicly documented guarantee on which algorithms can rely on.

Thanks!

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia dalla documentazione dell’API di Stack e dal relativo esempio di GetEnumerator(), quindi confronta la formulazione con l’output LIFO segnalato e la discussione collegata su StackOverflow. L’issue è completata quando la documentazione indica chiaramente, in seguito a una decisione dei maintainer, se l’ordine di enumerazione è una garanzia supportata.

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

Valutazione

Stack tecnologico
csharp
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
4/5
Tempo stimato
3-5 giorni
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.