Skip to content

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

@theodorzoulias

Description

@theodorzoulias

Hi! I used the Stack<T> collection recently to implement an algorithm, whose correctness now depends on the Stack<T> 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, where the accepted answer (by Matthew Watson) includes this excerpt:

The Stack.GetEnumerator() strongly implies that LIFO order is used.

If you look at the example for Stack<T>.GetEnumerator() in Microsoft's documentation 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityarea-System.Collectionshelp wantedGood for community contributors to help [up-for-grabs]

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions