Lesson #23 - Working with collections(Queue)

It represents a first-in, first-out collection. It is used when you need to create an array that supports the logic of first-in, first-out of elements. In this article, I 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();

}

}




0 views0 comments