Member-only story

Queue in C# with Examples

Shahzad Aslam
3 min readApr 11, 2024

--

Queue is related to System.Collections.Generic namespace. In, Queue Elements are added to the back (enqueue) and removed from the front (dequeue) of the queue. This is a First-In-First-Out (FIFO) data structure approach.

Queue in C# with Examples

Features with queue in C#:

Creating a Queue:

You need to import the System.Collections.Generic namespace to work with the Queue. To create a Queue, you can use the following code:

Example

using System.Collections.Generic;
Queue<int> queuefruitprices = new Queue<int>();
Queue<string> queuefruitnames = new Queue<string>();

Enqueueing Elements:

You can add elements to the back of the Queue using the Enqueue method:

Example

queuefruitprices.Enqueue(12);
queuefruitprices.Enqueue(16);
queuefruitprices.Enqueue(22);
queuefruitprices.Enqueue(28);
queuefruitprices.Enqueue(9);
queuefruitnames.Enqueue("Apple");
queuefruitnames.Enqueue("Mango");
queuefruitnames.Enqueue("Cherry");
queuefruitnames.Enqueue("Orange");
queuefruitnames.Enqueue("Grape");
queuefruitnames.Enqueue("Kiwi");

Dequeuing Elements:

You can remove elements from the front of the Queue using the Dequeue method:

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

Responses (1)