Home > Article > Backend Development > Queue in C#
A queue is a collection of object it represents in the form of FIFO (First-In-First-Out) order the element which is added first will come out first, in C# Queue collection class present in the namespace System.Collection. Queue stores the element in FIFO order in which we can retrieve in a first-in, first-out manner of accessing elements. A queue is exactly just opposite to Stack Collection, where Stack is LIFO (Last-In-First-Out). The collection of Queue allows numerous null and duplicate values. Queue uses two methods called Enqueue() and Dequeue() which used for adding and retrieving values respectively.
Syntax:
The queue is created using the data type called Queue. Here the “new” keyword is used for creating an object of the queue. In queue collection for adding an item, we using the Enqueue method and for deleting an item we using the Dequeue method.
Queue QueueObject = new Queue() // creation of Queue
QueueObject.Enqueue(element) // to add element to Queue
QueueObject.Dequeue() //to remove element to Queue
Queue present in the form of FIFO (First-In-First-Out) it’s a collection of objects, this process is used when we need to access in first-in, first-out access of items. The queue is a non-generic it uses the type of collection which is defined in the System.Collections namespace. In general, a queue is useful when we use the information in the way which we stored in the queue collection.
The Queue implements through the interfaces called IEnumerable, ICloneable, ICollection. For the reference types, it accepts the null valid values. In queue collection for adding an item, we using the Enqueue method and for deleting an item we using the Dequeue method when adding an item to queue the total capacity is automatically increase for required internal memory.
Example:
using System; using System.Collections; public class QueueProgram { static public void Main() { // to create a queue - using Queue class Queue _objQueue = new Queue(); // to add an elements in Queue - using Enqueue() method _objQueue.Enqueue("DotNet"); _objQueue.Enqueue("SQL"); _objQueue.Enqueue("Java"); _objQueue.Enqueue("PHP"); _objQueue.Enqueue("Android"); Console.WriteLine("Working Process of Queue\n"); Console.WriteLine("Number of Elements Present in Object(_objQueue) : {0}", _objQueue.Count); // to obtain the topmost element of _objQueue - using Dequeue method Console.WriteLine("\nTo Get the topmost element in Queue" + " is : {0}", _objQueue.Dequeue()); Console.WriteLine("\nNumber of Elements Present in Object(_objQueue) : {0}", _objQueue.Count); // to obtain the topmost element of _objQueue - using Peek method Console.WriteLine("\nTo Get the topmost element in Queue is : {0}", _objQueue.Peek()); Console.WriteLine("\nNumber of Elements Present in Object(_objQueue) : {0}", _objQueue.Count); // to check hether the element is present in the Queue if (_objQueue.Contains("SQL") == true) { Console.WriteLine("\nElement is Present !"); } else { Console.WriteLine("\nElement is not Present !"); } } }
In the above program, we declare the Queue as _ objQueue to hold the items of Queue. For adding the new element we using the method Enqueue() and for deleting the element we using the Dequeue() method. The property Count is used to get the total number of elements in the queue, the return value of this property is a number. Another method Contains() is used to check whether the given value/element is present, it returns the bool value either true or false. The Peek() is used to obtain the topmost value in the queue collection.
Output:
From the above output, it shows that the items of the Queue are displayed. Firstly it displays the total number of elements present in the queue by using the Count() method and then it displays the topmost element by using the Peek() method. By using the Contains() method it checks whether the element present in the queue collection.
In Queue class it consists of constructors that are used to create a queue.
Let’s see the following functions list which is commonly used methods of the Queue class −
In this article, we came to know the Queue() usage in C#, it is based on the FIFO concept, for adding and deleting the queue we using the Enqueue() and Dequeue() methods respectively.
The above is the detailed content of Queue in C#. For more information, please follow other related articles on the PHP Chinese website!