c#中的克隆队列 [英] Cloning queue in c#

查看:38
本文介绍了c#中的克隆队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

Queue<string> oldQueue = new Queue<string>();

oldQueue.Enqueue("One");
oldQueue.Enqueue("Two");
oldQueue.Enqueue("Three");

Queue newQueue = oldQueue;
string newString = newQueue.Dequeue();

问题是,一旦我将项目从 newQueue 中出列,该项目也会从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆