C# | Queue Class - GeeksforGeeks

20 Feb, 2019
Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called dequeue . This class comes under System.Collections namespace and implements ICollection, IEnumerable, and ICloneable interfaces.
Characteristics of Queue Class:
Enqueue adds an element to the end of the Queue.
Dequeue removes the oldest element from the start of the Queue.
Peek returns the oldest element that is at the start of the Queue but does not remove it from the Queue.
The capacity of a Queue is the number of elements the Queue can hold.
As elements are added to a Queue, the capacity is automatically increased as required by reallocating the internal array.
Queue accepts null as a valid value for reference types and allows duplicate elements.
Example:
// C# code to create a Queueusing System;using System.Collections;  class GFG {      // Driver code    public static void Main()    {          // Creating a Queue         Queue myQueue = new Queue();          // Inserting the elements into the Queue        myQueue.Enqueue("one");          // Displaying the count of elements        // contained in the Queue        Console.Write("Total number of elements in the Queue are : ");          Console.WriteLine(myQueue.Count);          myQueue.Enqueue("two");          // Displaying the count of elements        // contained in the Queue        Console.Write("Total number of elements in the Queue are : ");          Console.WriteLine(myQueue.Count);          myQueue.Enqueue("three");          // Displaying the count of elements        // contained in the Queue        Console.Write("Total number of elements in the Queue are : ");          Console.WriteLine(myQueue.Count);          myQueue.Enqueue("four");          // Displaying the count of elements        // contained in the Queue        Console.Write("Total number of elements in the Queue are : ");          Console.WriteLine(myQueue.Count);          myQueue.Enqueue("five");          // Displaying the count of elements        // contained in the Queue        Console.Write("Total number of elements in the Queue are : ");          Console.WriteLine(myQueue.Count);          myQueue.Enqueue("six");          // Displaying the count of elements        // contained in the Queue        Console.Write("Total number of elements in the Queue are : ");          Console.WriteLine(myQueue.Count);    }}
Total number of elements in the Queue are : 1
Total number of elements in the Queue are : 2
Total number of elements in the Queue are : 3
Total number of elements in the Queue are : 4
Total number of elements in the Queue are : 5
Total number of elements in the Queue are : 6

Example:
// C# code to Get the number of// elements contained in the Queueusing System;using System.Collections;  class GFG {      // Driver code    public static void Main()    {          // Creating a Queue        Queue myQueue = new Queue();          // Inserting the elements into the Queue        myQueue.Enqueue("Chandigarh");        myQueue.Enqueue("Delhi");        myQueue.Enqueue("Noida");        myQueue.Enqueue("Himachal");        myQueue.Enqueue("Punjab");        myQueue.Enqueue("Jammu");          // Displaying the count of elements        // contained in the Queue        Console.Write("Total number of elements in the Queue are : ");          Console.WriteLine(myQueue.Count);    }}
Total number of elements in the Queue are : 6

Example 1:
// C# code to Check if a Queue// contains an elementusing System;using System.Collections;  class GFG {      // Driver code    public static void Main()    {          // Creating a Queue        Queue myQueue = new Queue();          // Inserting the elements into the Queue        myQueue.Enqueue(5);        myQueue.Enqueue(10);        myQueue.Enqueue(15);        myQueue.Enqueue(20);        myQueue.Enqueue(25);          // Checking whether the element is        // present in the Queue or not        // The function returns True if the        // element is present in the Queue, else        // returns False        Console.WriteLine(myQueue.Contains(7));    }}
False

Example 2:
// C# code to Convert Queue to arrayusing System;using System.Collections;  class GFG {      // Driver code    public static void Main()    {          // Creating a Queue         Queue myQueue = new Queue();          // Inserting the elements into the Queue        myQueue.Enqueue("Geeks");        myQueue.Enqueue("Geeks Classes");        myQueue.Enqueue("Noida");        myQueue.Enqueue("Data Structures");        myQueue.Enqueue("GeeksforGeeks");          // Converting the Queue        // into object array        Object[] arr = myQueue.ToArray();          // Displaying the elements in array        foreach(Object obj in arr)        {            Console.WriteLine(obj);        }    }}
Geeks
Geeks Classes
Noida
Data Structures
GeeksforGeeks

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.queue?view=netframework-4.7.2
CSharp-Collections-Namespace
CSharp-Collections-Queue
CSharp-Queue-Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between Abstract Class and Interface in C#
String.Split() Method in C# with Examples
C# | How to check whether a List contains a specified element
C# Dictionary with examples
C# | IsNullOrEmpty() Method
C# | Delegates
C# | Method Overriding
C# | Arrays of Strings
C# | Abstract Classes
Difference between Ref and Out keywords in C#