在C#中克隆队列 [英] Cloning queue in c#
本文介绍了在C#中克隆队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
Queue<string> oldQueue = new Queue<string>();
oldQueue.Enqueue("One");
oldQueue.Enqueue("Two");
oldQueue.Enqueue("Three");
Queue newQueue = oldQueue;
string newString = newQueue.Dequeue();
问题在于,一旦我从 newQueue $ c中将项目出队, $ c>,该项目也会从
oldQueue
中出队。
是否可以通过某种方式克隆队列,从而从一个队列中删除某项将使其克隆队列保持不变?
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?
推荐答案
Queue是引用类型,因此newQueue和oldQueue持有指向同一对象的指针。它们是相同的;-)
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:
这篇关于在C#中克隆队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文