How to Add or Remove Elements in Queue Using C#

Hi Guy’s, Here is the article for Queue in C#. Queue is the Class and it represents First-in, First-out collection of objects.
Queue Constructor uses to Initializes a new instance of the System.Collections namespace. Queue is Class so we can create the object of class with the help of object we can perform various operation such as Enqueue,Count,Clear, Dequeue,Clone,Clear etc..

Code Explanation :-
Queue q1 = new Queue(); Queue is the class, create q1 object of Queue class by using new keyword and Queue Constructor.
q1.Enqueue(10); q1 calls Enqueue method with the help of dot operator. Enqueue method used to Adds an object to the end of the Queue.
Console.WriteLine(“Total Number of Elements in the Queue ” + q1.Count);
Count method is used to gets the number of elements contained in the Queue

Console.WriteLine(“Peek Position Number “+ q1.Peek());
Peek method is used to returns the object at the beginning of the Queue without removing it.
Console.WriteLine(“Dequeue {0}”, q1.Dequeue());
Dequeue method is used to removes and returns the object at the beginning of the Queue.

Happy Coding… Thanks.

Post Author: adama