top of page

C# Tutorial For Beginners: Working with collections(Queue)

Updated: Jan 19, 2022

It represents a first-in, first-out collection. It is used to create an array that supports the logic of first-in, first-out of elements. This article will provide code examples for its main properties and methods.


Note: the syntax of adding and removing items in the list, we will use the ‘enqueue’ (add item) and ‘deque’ (remove item).


Code Example:


class Program

{

static void Main(string[] args)

{

//Creating a new Queue

Queue QueueLearning = new Queue();


//Adding elements

QueueLearning.Enqueue("Ql_1");

QueueLearning.Enqueue("Ql_2");

QueueLearning.Enqueue("Ql_3");

QueueLearning.Enqueue("Ql_4");


//Determines whether an element is in the Queue

Console.WriteLine(QueueLearning.Contains("Ql_4"));//Returns True


//Returns the first object in the Queue

Console.WriteLine(QueueLearning.Peek()); //"Ql_1"


//Remove the first element from the queue

QueueLearning.Dequeue();//"Ql_1"

//Remove all objects from the Queue

QueueLearning.Clear();

}

}




1 view0 comments
bottom of page