Advertisement
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 | 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. |
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
//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);
}
}
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
//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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement