Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6551207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:18:28+00:00 2026-05-25T12:18:28+00:00

I have few stacks in my code that I use to keep track of

  • 0

I have few stacks in my code that I use to keep track of my logical location. At certain times I need to duplicate the stacks, but I can’t seem to clone them in such way that it preserves the order. I only need shallow duplication (references, not object).

What would be the proper way to do it? Or should I use some other sort of stacks?

I saw this post Stack Clone Problem: .NET Bug or Expected Behaviour?, but not sure how to setup clone method for the Stack<T> class.

I use System.Collection.Generic.Stack<T>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:18:29+00:00Added an answer on May 25, 2026 at 12:18 pm
    var clonedStack = new Stack<T>(new Stack<T>(oldStack));
    

    You can write this as an extension method as

    public static Stack<T> Clone<T>(this Stack<T> stack) {
        Contract.Requires(stack != null);
        return new Stack<T>(new Stack<T>(stack));
    }
    

    This is necessary because the Stack<T> constructor that we are using here is Stack<T>(IEnumerable<T> source) and of course when you iterate over the IEnumerable<T> implementation for a Stack<T> it is going to repeatedly pop items from the stack thus feeding them to you in reverse of the order that you want them to go onto the new stack. Therefore, doing this process twice will result in the stack being in the correct order.

    Alternatively:

    var clonedStack = new Stack<T>(oldStack.Reverse());
    
    
    public static Stack<T> Clone<T>(this Stack<T> stack) {
        Contract.Requires(stack != null);
        return new Stack<T>(stack.Reverse());
    }
    

    Again, we have to walk the stack in the reverse order from the output from iterating over the stack.

    I doubt there is a performance difference between these two methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen a few questions that are like mine, but none that actually
I have few text and excel file, from which I need to import data
I have a computational algebra task I need to code up. The problem is
I'm a beginner in assembly language and have noticed that the x86 code emitted
I have run into this issue too many times and need this to be
I have a need for a high-performance string hashing function in python that produces
I have the following code in a kernel module that walks up the process
I have a few doubts which i am already sure of it but still
I have to code a simple control in .Net that draws geometry that looks
I have tried to figure this out and googled through a fair few stack

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.