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>();
queue.Enqueue("A");
queue.Enqueue("B");
queue.Enqueue("C");
//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
queue.Enqueue("B");
queue.Enqueue("C");
// check below if given element is present or not and accordingly perform your action.
if (queue1.Contains("A"))
{
Console.WriteLine("Element A is Present");
}
else
{
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
queue.Enqueue("B");
queue.Enqueue("C");
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.