Skip to content

ConcurrentStack<T> has no DeepClone overload, so the clone comes back as IEnumerable<T> and rebuilding a stack from it reverses pop order #102

Closed

Description

@matt-edmondson

What's wrong

Stack<T> has a dedicated overload that keeps pop order by rebuilding from the reversed enumeration:

public static Stack<T> DeepClone<T>(this Stack<T> source)
{
Ensure.NotNull(source);
return new(source.Reverse().Select(DeepClone));
}

ConcurrentStack<T> has no such overload. stack.DeepClone() binds to the IEnumerable<T> overload:

public static IEnumerable<T> DeepClone<T>(this IEnumerable<T> source)
{
Ensure.NotNull(source);
return [.. source.Select(DeepClone)];
}

That overload returns the elements top-first as an IEnumerable<T>. The obvious way to get a stack back is to pass that sequence to the constructor, which pushes the elements in that order and silently reverses the stack.

Repro (net10.0)

var cs = new ConcurrentStack<int>(); cs.Push(1); cs.Push(2); cs.Push(3);
var clone = new ConcurrentStack<int>(cs.DeepClone());
// cs pops 3,2,1 ; clone pops 1,2,3

var st = new Stack<int>(); st.Push(1); st.Push(2); st.Push(3);
// st.DeepClone() pops 3,2,1 (correct, thanks to the dedicated overload)

Why it matters

The library already treats stack reversal as a bug for Stack<T>, and #94 does the same for ImmutableStack<T>. ConcurrentStack<T> is the remaining BCL LIFO type where it still happens silently. #94 covers only the Immutable* types, so it does not cover this one.

Suggested fix / acceptance criteria

Activity

  1. added
    enhancement
    on Oct 6, 2026
  2. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions