C# Queue

Queue Collections come under System.Collection.Generic namespace. It stores in the FIFO form i.e. First In First Out, unlike Stack which stores in the LIFO form. Enqueue is when data is inserted in the queue, whereas Dequeue is when it is removed.

Create a Queue and Enqueue

Add elements in the Queue with the Enqueue() method in C#. Enqueue means inserting elements to the end of the queue. To create a Queue in C#, with string elements, use <string>. Set the type in angular braces as shown below:

Let us see an example and create a queue with some string elements:



To return the first element (beginning of the queue) from the queue and remove it, use the Dequeue() method in C#. Let us see an example:


Count the number of elements

The Count property is used to count the number of elements in the Queue. Let us see an example:


Check for an element in the queue

The Contains() method is used to check whether an element exists in the queue or not. Let us see an example:


Peek an element

To return the first element from the top of the queue, without removing it, use the Peek() method. Let us see an example:


Read More

C# - Installation and Setup
C# vs Java - Difference
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading