How to cast or convert List of objects to queue of objects

GigaPr picture GigaPr · Aug 12, 2010 · Viewed 26.2k times · Source

How can one convert a list of objects to a queue thereby maintaining the same order?

Answer

Ryan Brunner picture Ryan Brunner · Aug 12, 2010

Queue has a constructor that takes in an ICollection. You can pass your list into the queue to initialize it with the same elements:

var queue = new Queue<T>(list);    // where 'T' is the lists data type.