In this post we will learn about C# | Queue with Examples. Queue is a generic class that arranges elements of a specified data type using ‘First in First Out’ (FIFO) principle. Element which is added at first will be removed at first. We can take Queue<T> where T can be anything like object, string, int to hold specific data in it. Queue can be found in  System.Collection.Generic namespace.

A layman example of a queue is a line of people standing for bus ticket.

C# | Queue Constructors

In below code we will create an empty queue of type “string”. It will hold data of type string only. Further if you want to add element in it then you can use Enqueue() method of Queue.

Queue<string> queue = new Queue<string>();

//how to pass array elements into Queue instead of adding them one by one.

string[] letters = { "A","B", "C","D", "E","F" };  //  array of type string  
Queue<string> queue = new Queue<string>(letters);  // copied all elements from array to Queue.

Similarly, you can create queue and choose the data type as per your need.

Queue<string> queue = new Queue<string>();          // string type Queue
Queue<int> queue = new Queue<int>();                // int type Queue
Queue<Object> queue = new Queue<Object>();          // you can also pass Object

Is C# queue thread-safe?

In nutshell we can say that queue is NOT safe to use with multi-threading environment. If you have multi-threading environment, then you can use ConcurrentQueue class which is the wrapper class of Queue class and is safe for multi-threading environment.

Queue and ConcurrentQueue both follow FIFO principle. ConcurrentQueue class exists in System.Collections.Concurrent namespace whereas Queue class belongs to System.Collections.Generic namespace.

How to specify initial capacity of Queue

Let’s check out below how to specify initial capacity of Queue.

Queue<string> queue = new Queue<string>(5);       // 5 length is specified

Queue.Enqueue Method

The Queue.Enqueue method is used to adds an object of specific data type at the end of the Queue<T>.

Queue.Dequeue Method

The Queue.Dequeue() Method is used to remove an object of specific data type from the queue. It works on FIFO principle and remove the element which was added at first.

Queue.Contain() method

Queue.Contain() method is used to determines whether given element is present in the Queue<T> or not. If you want to perform an action and before that you want to check whether element is present in the Queue, then this method is very helpful.

Queue<string> queue = new Queue<string>();  // create Queue
queue.Enqueue("A");                         // add elements in Queue

// check below if given element is present or not and accordingly perform your action.
if (queue1.Contains("A"))
    Console.WriteLine("Element A is Present");
    Console.WriteLine("Element A is Absent");

Queue.Clear() method

It is used to clear all available element from that Queue. Once after running Queue.Clear() method count will come as zero which indicates all elements are removed from ‘Queue’ in one go using this method.

Queue.Peek method

In Queue, Queue.Peek() method is used to returns the object which is present at the beginning of the Queue<T>.

     Queue<string> queue = new Queue<string>();   // creating Queue
     queue.Enqueue("A");                          //adding elements
     Console.WriteLine("Output of Peek method is:" + queue.Peek());

Queue.ToArray() method

This method is used to copy the Queue’s all elements into a new array object. After this you can access all available elements form Array too.

In this post we learned about Queue class and also about its various methods/properties.

You can check more such articles in our C# Section. You may also like our Interview Section too.

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can