Queue reрresents а first-in, first оut соlleсtiоn оf оbjeсt. It is used when yоu need а first-in, first-оut ассess оf items. When yоu аdd аn item in the list, it is саlled enqueue, аnd when yоu remоve аn item, it is саlled dequeue . This сlаss соmes under System.Соlleсtiоns nаmesрасe аnd imрlements IСоlleсtiоn, IEnumerаble, аnd IСlоneаble interfасes.
Queue queue =
new
Queue();
For adding elements we can use the method of Enqueue()
;
Queue myQueue = new Queue();
myQueue.Enqueue("BMW");
myQueue.Enqueue("Audi");
myQueue.Enqueue("Toyota");
myQueue.Enqueue("KIA");
Queue myQueue = new Queue();
// Add element in the queue
myQueue.Enqueue("BMW");
myQueue.Enqueue("Audi");
myQueue.Enqueue("Toyota");
myQueue.Enqueue("KIA");
Object[] array = myQueue.ToArray();
// Display the value
foreach (Object element in array)
{
Console.WriteLine(element);
}
BMW
Audi
Toyota
KIA
To get the size of the Queue there's a method of count that will count the member/element of the Queue.
Queue myQueue = new Queue();
// Add element in the queue
myQueue.Enqueue("BMW");
myQueue.Enqueue("Audi");
myQueue.Enqueue("Toyota");
myQueue.Enqueue("KIA");
Object[] array = myQueue.ToArray();
Console.WriteLine("Total Count:" + myQueue.Count); // Total count is 4
Total Count : 4
The Dequeue()
and Peek()
methods are used to retrieve the first item in a queue set. Dequeue () removes and returns the first item in a queue because the queue stores items in FIFO order. Calling the Dequeue () method in an empty queue throws an InvalidOperation exception. Therefore, always check that the total number of queues is greater than zero before calling it.
Queue myQueue = new Queue();
myQueue.Enqueue("BMW");
myQueue.Enqueue("Audi");
myQueue.Enqueue("Toyota");
myQueue.Enqueue("KIA");
Object[] array = myQueue.ToArray();
Console.WriteLine("Total Count:" + myQueue.Count); // Total count is 4
// Display the value
foreach (Object element in array)
{
Console.WriteLine(element);
}
while (myQueue.Count > 0)
myQueue.Dequeue(); // Dequeue the element one by one
Console.WriteLine("Total Count:" + myQueue.Count); // Total count is 0 after dequeue
Total Count: 4
BMW
Audi
Toyota
KIA
Total Count: 0
Properties | Usage |
---|---|
Count |
Gets the number of elements contained in the Queue<T> |
Method | Usage |
---|---|
Clear |
Removes all objects from the Queue |
Contain |
Determines whether an element is in the Queue<T>. |
CopyTo(T[], Int32) |
Copies the Queue<T> elements to an existing one-dimensional Array, starting at the specified array index. |
Dequeue() |
Removes and returns the object at the beginning of the Queue<T>. |
Enqueue(T) |
Adds an object to the end of the Queue<T>. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetEnumerator() |
Returns an enumerator that iterates through the Queue<T>. |
GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
Peek() |
Returns the object at the beginning of the Queue<T> without removing it. |
ToArray() |
Copies the Queue<T> elements to a new array. |
ToString() |
Returns a string that represents the current object. (Inherited from Object) |
TrimExcess() |
Sets the capacity to the actual number of elements in the Queue<T>, if that number is less than 90 percent of current capacity. |
TryDequeue<T> |
Removes the object at the beginning of the Queue<T>, and copies it to the result parameter. |
TryPeek(T) |
Returns a value that indicates whether there is an object at the beginning of the Queue<T>, and if one is present, copies it to the parameter. The object is not removed from the Queue<T>. |