Queue C# with Example



Queue C# with Example

// Initalize a new queue of integers 
var queue = new Queue(); 
// Add some data 
queue.Enqueue(6); 
queue.Enqueue(4); 
queue.Enqueue(9); 
// Elements in a queue are stored in "first in, first out" order. 
// The queue from first to last is: 6, 4, 9 
// View the next element in the queue, without removing it. 
Console.WriteLine(queue.Peek()); // prints 6 
// Removes the first element in the queue, and returns it. 
Console.WriteLine(queue.Dequeue()); // prints 6 
Console.WriteLine(queue.Dequeue()); // prints 4 
Console.WriteLine(queue.Dequeue()); // prints 9 
Thread safe heads up! Use ConcurrentQueue in multi-thread environments. 
 

0 Comment's

Comment Form

Submit Comment