Advertisement



< Prev
Next >



C# Queue





In C#, Queue is a non-generic collection class which is used to maintain a first-in, first-out(FIFO) collection of objects i.e. the first element to enter the Queue is the first one to be removed from it.

In other words, the Queue can be thought of as people waiting in a queue to enter a shopping complex, the first person at the front of the queue is the first one to exit the queue and enter the shopping complex, while a new person is always added at the back/end of the queue.

Note: Queue implements the ICollection, IEnumerable, and ICloneable interfaces.



Important features of a Queue







Commonly used constructors of Queue


Constructor Description
Queue()
This constructor creates an empty instance of the Queue class by using the default initial capacity.

The capacity of a Queue is the number of elements it can hold, as the new elements are added to a Queue, its capacity is automatically increased.

Queue(int capacity)
This constructor creates a Queue that is empty and has the specified initial capacity.

Queue(ICollection)
This constructor creates a Queue, initialized with the elements of an ICollection.






Properties of Queue:


Properties Description
Count
This property gives total number of elements in a Queue.

IsSynchronzied
This property determines whether a Queue is thread-safe or not.

SyncRoot
Thie property gets an object that can be used to synchronize the access to a Queue.






Some commonly used methods of Queue class


Methods Description
Object Dequeue()
This method removes and returns an object at the front/beginning of a Queue.

void Enqueue(Object ob)
This method inserts an object at end of a Queue.

Object Peek()
This method returns an object at the top of a Queue, without removing it.

bool Contains(Object o)
This method checks if a Queue contains a specific Object.

void Clear()
This method removes all the elements from a Queue.

IEnumerator GetEnumerator()
This method returns an IEnumerator to enumerate an entire Queue.

Object[ ] ToArray()
This method returns an Object array, containing all the elements of a Queue.




Advertisement




An example of Queue


In this program we will first create a Queue to hold objects and next, we will add and remove objects from this Queue by using its methods described above in the table. Let's see the code now -:

//C# Example of a Queue

using System;
using System.Collections;

class Queue1
{
public static void Main(String[] ar)
{
	//Creating a Queue
	Queue q = new Queue();
	

	//Calling the Enqueue() method to push elements into the Queue
	//New element is always added to the end of the Queue
	q.Enqueue(10);
	q.Enqueue(23);
	q.Enqueue(16);
	q.Enqueue(5);
	q.Enqueue(29);

	//Printing the contents of Queue
	Console.WriteLine("The contents of Queue: ");
	foreach(int element in q)
		Console.WriteLine(element);


	//Calling the Dequeue() method to remove the front element of the Queue
	Console.WriteLine("Removing the front element = "+ q.Dequeue());
	Console.WriteLine("Removing the next front element = "+ q.Dequeue());


	//Calling the Peek() method which only gets the front element of the Queue, without removing it
	Console.WriteLine("Peeking at the current front element = "+ q.Peek());


	//Printing the updated contents of Queue
	Console.WriteLine("Updated contents of Queue: ");
	foreach(int element in q)
		Console.WriteLine(element);
}
}


Output is - :


The contents of Queue:
10
23
16
5
29
Removing the front element = 10
Removing the next front element = 23
Peeking at the front element = 16
Updated contents of Queue:
16
5
29


Program Analysis










Converting a Queue to an Object array


In the upcoming program, we will call the ToArray() method, which returns an Object array, containing all the elements of an invoked Queue.

//C# Example of a Queue

using System;
using System.Collections;

class Queue1
{
public static void Main(String[] ar)
{
	//Creating a Queue
	Queue q = new Queue();
	

	//Calling the Enqueue() method to push elements into the Queue
	//New element is always added to the end of the Queue
	q.Enqueue(10);
	q.Enqueue(23);
	q.Enqueue(16);
	q.Enqueue(5);
	q.Enqueue(29);

	//Printing the contents of Queue
	Console.WriteLine("The contents of Queue: ");
	foreach(int element in q)
		Console.WriteLine(element);

	//Calling the ToArray() method of Queue
	Object[] obArr = q.ToArray();


	//Printing the contents of Object array
	Console.WriteLine("The contents of Object array initialized from the elements of Queue: ");
	foreach(Object element in obArr)
		Console.WriteLine(element);
}
}


Output is :


The contents of Queue:
10
23
16
5
29
The contents of Object array initialized from the elements of Queue:
10
23
16
5
29




Program Analysis






Please share this article -





< Prev
Next >
< C# Stack
C# SortedList >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement