A Queue in C# represents a first-in, first-out (FIFO) collection of objects. An example of a queue is a line of people waiting.
The Queue<T> class in the System.Collection.Generic namespace represents a queue in C#, where T specifies the type of elements in the queue. In this article and code examples, I explain the use of Queue in C#, Queue class properties and methods, and how to use these methods in C#.
C# Queue Constructors
The Queue<T> class has several overloaded constructors.
Queue<T> Constructor - Initializes a new instance of the Queue<T> class that is empty. The next lines show how we create the empty queue.
Queue<string> queue = newQueue<string>();
Queue<T> Constructor (IEnumerable<T>) - Initializes a new instance of the Queue<T> class that contains elements copied from the specified collection.
string[] courses = { "MCA","MBA", "BCA","BBA", "BTech","MTech" };
Queue<string> queue = newQueue<string>(courses);
Queue<T> Constructor (Int32) - Initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.
Queue<string> queue = newQueue<string>(4);
Queue. Count Property
The Count property gets the number of elements of Queue<T>. The following code sample creates a queue of strings and gets the item count in the collection.
namespace Queue {
classProgram {
staticvoid Main(string[] args) {
string[] courses = {
"MCA",
"MBA",
"BCA",
"BBA",
"BTech",
"MTech"
};
Queue < string > queue1 = newQueue < string > ();
Queue < string > queue2 = newQueue < string > (courses);
Queue < string > queue3 = newQueue < string > (4);
Console.WriteLine("Number of elements in queue1:" + queue1.Count());
Console.WriteLine("Number of elements in queue2:" + queue2.Count());
Console.WriteLine("Number of elements in queue3:" + queue3.Count());
}
}
}
The output looks like this,

Queue.Enqueue Method
The Queue. The enqueue method adds an object to the end of the Queue<T>. For example,
namespace Queue {
classProgram {
staticvoid Main(string[] args) {
Queue < string > queue1 = newQueue < string > ();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The elements in the queue are:");
foreach(string s in queue1) {
Console.WriteLine(s);
}
}
}
}
Output

Queue.Dequeue() method
Removes and returns the object at the beginning of the Queue<T>. For example,
namespace Queue {
classProgram {
staticvoid Main(string[] args) {
Queue < string > queue1 = newQueue < string > ();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The elements in the queue are:");
foreach(string s in queue1) {
Console.WriteLine(s);
}
queue1.Dequeue(); //Removes the first element that enter in the queue here the first element is MCA
queue1.Dequeue(); //Removes MBA
Console.WriteLine("After removal the elements in the queue are:");
foreach(string s in queue1) {
Console.WriteLine(s);
}
}
}
}
Output

Queue.Contain() method
Determines whether an element is in the Queue<T>. For example,
namespace Queue {
classProgram {
staticvoid Main(string[] args) {
Queue < string > queue1 = newQueue < string > ();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The elements in the queue are:");
foreach(string s in queue1) {
Console.WriteLine(s);
}
Console.WriteLine("The element MCA is contain in the queue:" + queue1.Contains("MCA"));
Console.WriteLine("The element BCA is contain in the queue:" + queue1.Contains("BCA"));
Console.WriteLine("The element MTech is contain in the queue:" + queue1.Contains("MTech"));
}
}
}
Output

Queue.Clear() method
Removes all objects from theQueue<T>. For example,
namespace Queue {
classProgram {
staticvoid Main(string[] args) {
Queue < string > queue1 = newQueue < string > ();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The elements in the queue are:" + queue1.Count());
queue1.Clear();
Console.WriteLine("The elements in the queue are after the clear method:" + queue1.Count());
}
}
}
Output

Queue.Peek method
Returns the object at the beginning of the Queue<T> without removing it. For example,
namespace Queue {
classProgram {
staticvoid Main(string[] args) {
Queue < string > queue1 = newQueue < string > ();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("Peek the first item from the queue is:" + queue1.Peek());
queue1.Dequeue();
Console.WriteLine("Peek the next item from the queue is:" + queue1.Peek());
}
}
}
Output

Queue.ToArray() method
Copies the Queue<T> elements to a new array. For example,
namespace Queue {
classProgram {
staticvoid Main(string[] args) {
Queue < string > queue1 = newQueue < string > ();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The queue elements are:");
foreach(string s in queue1) {
Console.WriteLine(s);
}
Queue < string > queue2 = newQueue < string > (queue1.ToArray());
Console.WriteLine("\nContents of the copy");
foreach(string n in queue2) {
Console.WriteLine(n);
}
}
}
}
Output

Summary
In this article, I explained the Queue<> class and its various methods and properties of it.
Here are recommended articles on collections.

Harun OconnellPosted Feb 1, 2023, 6:10 PM
STACK: public void Add(List t) { number++; list.Push(t); } public Paczka DeleteOne() { number--; return list.Pop(); } public void clearr() { list.Clear(); number = 0; } public int givemethenumber() { return list.Count; } public Paczka current() { return list.Peek(); }
Harun OconnellPosted Feb 1, 2023, 6:07 PM
Public void Clearr() { list.Clear(); number= 0; } public int GiveTheNumber() { return list.Count; } public List showcurrent() { return list.Peek(); }
Harun OconnellPosted Feb 1, 2023, 6:06 PM
Public List Delete() { if (number == 0) return null; number--; return list.Dequeue(); }
Harun OconnellPosted Feb 1, 2023, 6:05 PM
Hi, it's for QUEUE: adding: public void Add(List t) { number++; list.Enqueue(t); }
Jaime StuardoPosted Nov 17, 2022, 8:08 PM
Hello.... when converting a Queue to a LIst, elements are also removed from the queue?
Rikam PalkarPosted Oct 26, 2021, 4:16 AM
Job well done.
Lokesh VarmanPosted Apr 9, 2021, 4:26 AM
Can you please help me in implementing a queue in a process where the elements to be added into the queue from a different process
vinay ayinapurapuPosted Mar 8, 2020, 11:16 AM
Can you please tell us how to return queue to main method? The scenario is i wrote a method, that return queue of integers and want to process these values in main method. I tried both these methods declaration but giving null values. public static Queue<int> Bdivision(int n, int p) public static Queue Bdivision(int n, int p)
Pankajkumar PatelPosted Sep 27, 2019, 12:27 AM
Nice article
Джон СинаPosted May 22, 2019, 12:43 AM
Kindergarden-level article, totally useless.
Adonis MendozaPosted Dec 30, 2018, 5:22 PM
When it is necessary to use these?
Rehman ShahidPosted Dec 30, 2018, 12:51 AM
Nice Article..............
Khaja MoizuddinPosted Oct 30, 2018, 11:03 PM
Nicely written Richa ......