This tutorial is about queues in C#. Queue follows the First In First Out (FIFO) principle

Don't forget to check out our site http://howtech.tv/ for more free how-to videos!
http://youtube.com/ithowtovids - our feed
http://www.facebook.com/howtechtv - join us on facebook
https://plus.google.com/103440382717658277879 - our group in Google+

Introduction

Queues in C# are similar to stacks and are present in System.Collections namespace. The difference is that queue follows the First In First Out(FIFO) principle. Remember stack operates on Last In First Out (LIFO) principle. A queue in C# is used for sequential processing.

Step 1: Implementing Queue
When you remove elements from queues in C#, the first element added will be removed. It is the reverse of stack. Look into the code snippet below:
Queue Q = new Queue();
Q.Enqueue(1);//Add elements to queuq
Q.Enqueue(2);
foreach (int i in Q)//Displays the elements of the queue
Console.WriteLine(i);
Console.WriteLine(Q.Peek());//returns first element in the queue
Console.WriteLine(Q.Dequeue());//returns the first element and removes the referral
Console.Read();

Step 2: Manipulating Queue

Thus Enqueue() is the method used to populate the queue. Since the queue is populated in FIFO order, when you use the Peek() method, the first element is returned. Dequeue() also returns the first element. The difference between Peek() and Dequeue() is that Dequeue() removes the referral to the element thereby making the second element the first. Just like stacks, the queue can increase/decrease its capacity dynamically.

The number of elements in a queue can be found by using the count property:
int k = Q.Count;

Queue in C# accepts null values as a valid input and allows duplicate elements.