Advertisement



< Prev
Next >



C# Queue<T>





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

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

Note: Queue<T> implements the ICollection, IEnumerable and IEnumerable<T> interfaces and it is defined in the System.Collections.Generic namespace.



Important features of a Queue<T>







Commonly used constructors of Queue<T>


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

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

Queue<T>(int capacity)
This constructor initializes created a Queue<T> that is empty and has the specified initial capacity.

Queue<T>(IEnumerable<T>)
This constructor creates a Queue<T> initialized with the elements of an IEnumerable<T>.






Properties of Queue<T>:


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

ICollection.IsSynchronzied
This property determines whether access to the ICollection is synchronized (thread safe).

ICollecton.SyncRoot
Thie property gets an object that can be used to synchronize access to the ICollection.






Some commonly used methods of Queue<T> class


Methods Description
T Dequeue<T>()
This method removes and returns the object at the front/beginning of the Queue<T>.

void Enqueue<T>(T ob)
This method inserts an object at end of the Queue<T>.

T Peek()
This method returns the object at the top of the Queue<T> without removing it.

bool Contains(T o)
This method checks if Queue<T> contains a specific T.

void Clear()
This method removes all of the elements from the Queue<T>.

Queue<T>.Enumerator GetEnumerator()
This method returns an IEnumerator to enumerate the entire Queue<T>.

T[ ] ToArray()
This method returns an array of type T, containing all the elements of a Queue<T>.




Advertisement




An example of Queue<T>


In this program we have created a Queue<T> to hold int values, and because C# has a unified type system, so any primitive values like int, char, float, etc are implicitly boxed into an Object reference.

Next, we will add and remove objects from this Queue<T> using its methods described above in the table. Let's see the code now -:

//C# Example of a Queue<T>

using System;
using System.Collections.Generic;

class QueueEx
{
public static void Main(String[] ar)
{
	//Creating a Queue<T> of type int i.e. Queue<int> to hold int values
	//Where each int value is implicitly converted to an Object.
	Queue<int> dq = new Queue<int>();
	

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

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


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


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


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


Output is - :


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


Program Analysis










Converting a Queue<T> to an array


In the upcoming program, we will call the ToArray(), which returns an array of type T i.e. of type of the invoked Queue<T>, containing all the elements of the invoked Queue<T>.

//C# Example of a Queue<T> calling the ToArray() method

using System;
using System.Collections.Generic;

class QueueEx
{
public static void Main(String[] ar)
{
	//Creating a Queue<T> of char type i.e. Queue<char> to hold characters
	Queue<char> dq = new Queue<char>();
	

	//Calling the Enqueue() method to push elements into the Queue<T>
	//New element is always added to the end of the Queue<T>
	dq.Enqueue('e');
	dq.Enqueue('x');
	dq.Enqueue('b');
	dq.Enqueue('z');
	dq.Enqueue('e');

	//Printing the contents of Queue<char>
	Console.WriteLine("The contents of Queue<char>: ");
	foreach(char element in dq)
		Console.WriteLine(element);

	//Calling the ToArray() method of Queue<char>
	//Which returns a char array
	char[] chArr = dq.ToArray();


	//Printing the contents of char array
	Console.WriteLine("The contents of char array initialized from the elements of Queue<char>: ");
	foreach(char element in chArr)
		Console.WriteLine(element);
}
}


Output is :


The contents of Queue&;t;char>:
e
x
b
z
e
The contents of char array initialized from the elements of Queue<char>:
e
x
b
z
e




Program Analysis






Please share this article -





< Prev
Next >
< C# Generic Stack
C# Generic LinkedList >



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