Cloning queue in c#

c#, clone, queue

Solution

Queue is a reference type, so newQueue and oldQueue holds a pointer to the same object. They are the same ;-)

See this answer about how to do a "deep cloning" using reflection:

Deep Copy using Reflection in an Extension Method for Silverlight?

Problem

I have the following code: ``` Queue<string> oldQueue = new Queue<string>(); oldQueue.Enqueue("One"); oldQueue.Enqueue("Two"); oldQueue.Enqueue("Three"); Queue newQueue = oldQueue; string newString = newQueue.Dequeue(); ``` The problem is that once I Dequeue the item from `newQueue`, the item is also Dequeued from `oldQueue`. Is there a way to "clone" a queue in a way that removing an item from one queue will keep it's clone queue unchanged?

Original source

Related problems