Queue Class:
The Queue Works Like First-in, First Out (FIFO) System. Its mean, me can store Objects in a Queue from one end and can remove from other end.
Queues are useful when we need temporary storage for Information, that is, when we might want to discard an element after retrieving its value. We use Queue, if we need to access the information in the same order that it is stored in the collection. We can perform three main Operations on a Queue and its elements:
Queue collection allows multiple null and duplicate values. Queue class can hold any object. This class provide us some properties to work with Queue. The following table show some of the commonly used Properties of the Queue Class:
Property | Description |
---|---|
Count | Gets the Number of Elements contained in the Queue. |
The following table show some of the commonly used Methods of the Queue Class:
Methods | Description |
---|---|
Clear() | Removes all objects from the Queue. |
Contains() | Determines whether an element is in the Queue. |
Enqueue | Adds an object to the end of the Queue. |
Dequeue() | Removes and returns the object at the beginning of the Queue. |
Finalize() | Allows an object to try to free resources and perform other clean-up operations before it is reclaimed by garbage collection |
GetType() | Gets the Type of the current instance |
Peek() | Returns the object at the beginning of the Queue without removing it. |
ToArray() | Copies the Queue elements to a new array. |
ToString() | Returns a string that represents the current object |
TrimToSize() | Sets the capacity to the actual number of elements in the Queue. |
For complete list of properties and methods, please visit Microsoft's C# documentation. |
The Capacity of the Queue is the Number of Elements the Queue Can Hold. As Elements are added to a Queue, the capacity is automatically increased as required through reallocation. The Capacity can be decreased by using TrimToSize() method. Let's have a simple example, to know how we can create Queue, and add values to it and how to remove values. and how to access the values of Queue collection.
/*Example - Queue Class - InfoBrother*/
using System;
using System.Collections;
namespace QueueClass
{
class Program
{
static void Main(string[] args)
{
//create Queue.
Queue Que = new Queue();
//Add values in Queue.
Que.Enqueue('A');
Que.Enqueue('B');
Que.Enqueue('C');
Que.Enqueue('D');
Que.Enqueue('E');
Console.WriteLine("Current queue: ");
//Access Queue Values:
foreach (char c in Que)
{
Console.Write(c + " ");
}
Console.WriteLine();
//Add some more values at the end of the Queue:
Que.Enqueue('F');
Que.Enqueue('G');
Console.WriteLine("\nAfter Updating Queue: ");
//Access Queue Values:
foreach (char c in Que)
{
Console.Write(c + " ");
}
Console.WriteLine();
Console.WriteLine("\nRemoving some values from Queue. ");
//Remove Old value from the Queue: (i.e A)
char ch = (char)Que.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
//Remove Another value from the Queue: (i.e B)
ch = (char)Que.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
Console.WriteLine("\nAfter Removing some Queue Members: ");
//Access Queue Values:
foreach (char c in Que)
{
Console.Write(c + " ");
}
//Check Do we have Z in our Queue List:
Console.WriteLine("\n\nDo We have item \"Z\" in Our List? " + Que.Contains('z'));
//Check Do we have Z in our Queue List:
Console.WriteLine("Do We have item \"D\" in Our List? " + Que.Contains('D'));
//using peek(), Get the first item from Queue:
Console.WriteLine("\n\nThe first element is: " + Que.Peek());
//clear all elements from the Queue:
Que.Clear();
Console.WriteLine("\nAfter Removing all Members from Queue: ");
//Access Queue Values:
foreach (char c in Que)
{
Console.Write(c + " ");
}
Console.ReadKey();
}
}
}
Queue Class:
Current queue: A B C D E After Updating Queue: A B C D E F G Removing some values from Queue. The removed value: A The removed value: B After Removing some Queue Members: C D E F G Do We have item "Z" in Our List? False Do We have item "D" in Our List? True The first element is: C After Removing all Members from Queue: