vb.net 将列表转换为队列 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28927938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert List to Queue c#
提问by Stavros Afxentis
is it possible to convert a List to a Queue element?? Until now i used the code below but adding an extra loop is not necessary if there is a build up method in c#
是否可以将列表转换为队列元素?到目前为止,我使用了下面的代码,但是如果 c# 中有构建方法,则不需要添加额外的循环
queue1.Clear();
foreach (int val in list1)
{
queue1.Enqueue(val);
}
any solution?
任何解决方案?
回答by Yuval Itzchakov
Queue<T>has an overload which takes an IEnumerable<T>. Note it will iterate it anyway:
Queue<T>有一个重载,需要一个IEnumerable<T>. 请注意,它无论如何都会迭代它:
var queue = new Queue<string>(myStringList);
This is what it does internally:
这是它在内部所做的:
public Queue(IEnumerable<T> collection)
{
_array = new T[_DefaultCapacity];
_size = 0;
_version = 0;
using(IEnumerator<T> en = collection.GetEnumerator())
{
while(en.MoveNext()) {
Enqueue(en.Current);
}
}
}

