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 中出列,该项目也会从 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#中的克隆队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c#中的克隆队列
基础教程推荐
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
