How to create a Queue in C#

18 Feb, 2019
Queue() Constructor is used to initializes a new instance of the Queue class which will be empty, and will have the default initial capacity, and uses the default growth factor. Queue represents a first-in, first out collection of object. It is used when you need 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.
Syntax:
public Queue ();
Important Points:
The capacity of the Queue represents the number of elements that the Queue can hold. It will increase automatically through reallocation as elements are added to it.
TrimToSize method is used to decreased the capacity of the Queue.
When a greater capacity is required then the current capacity is multiplied by a number which is termed as the growth factor.
This constructor is an O(1) operation.
Example 1:
// C# Program to illustrate how// to create a Queueusing System;using System.Collections;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // qt is the Queue object        // Queue() is the constructor        // used to initializes a new         // instance of the Queue class        Queue qt = new Queue();          // Count property is used to get the        // number of elements in Queue        // It will give 0 as no elements         // are present currently        Console.WriteLine(qt.Count);    }}
0

Example 2:
// C# Program to illustrate how// to create a Queueusing System;using System.Collections;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // qt is the Queue object        // Queue() is the constructor        // used to initializes a new         // instance of the Queue class        Queue qt = new Queue();          Console.Write("Before Enqueue Method: ");                  // Count property is used to get the        // number of elements in Queue        // It will give 0 as no elements         // are present currently        Console.WriteLine(qt.Count);          // Inserting the elements        // into the Queue        qt.Enqueue("This");        qt.Enqueue("is");        qt.Enqueue("how");        qt.Enqueue("to");        qt.Enqueue("create");        qt.Enqueue("Queue");        qt.Enqueue("in");        qt.Enqueue("C#");          Console.Write("After Enqueue Method: ");                  // Count property is used to get the        // number of elements in qt        Console.WriteLine(qt.Count);    }}
Before Enqueue Method: 0
After Enqueue Method: 8

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.queue.-ctor?view=netframework-4.7.2#System_Collections_Queue__ctor
CSharp-Collections-Namespace
CSharp-Collections-Queue
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#
C# Dictionary with examples
C# | How to check whether a List contains a specified element
C# | Arrays of Strings
C# | IsNullOrEmpty() Method
String.Split() Method in C# with Examples
Introduction to .NET Framework
C# | Delegates
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework