Queue:
C# includes a Queue collection class in the System.Collection namespace. Queue stores the elements in FIFO style (First In First Out), exactly opposite of the Stack collection. It contains the elements in the order they were added.
Queue collection allows multiple null and duplicate values.
Use the Enqueue() method to add elements into Queue
The Dequeue() method returns and removes elements from the beginning of the Queue. Calling the Dequeue() method on an empty queue will throw an exception.
The Peek() method always returns top most element.
Properties of queue
Count - Returns the total count of elements in the Queue.
Methods of queue
- Enqueue: Adds an item into the queue.
- Dequeue: Removes and returns an item from the beginning of the queue.
- Peek: Returns an first item from the queue
- Contains: Checks whether an item is in the queue or not
- Clear: Removes all the items from the queue.
- TrimToSize: Sets the capacity of the queue to the actual number of items in the queue.
1.Enqueue: Enqueue method is used to add element in queue.you can add element of any datatype as it's a non-generic collection.
Example
class CollectionExamples
{
static void Main(string[] args)
{
Queue _queue = new Queue();
_queue.Enqueue(1);
_queue.Enqueue(2);
_queue.Enqueue(3);
_queue.Enqueue(3); //it allows duplicate
_queue.Enqueue(null); // it allows null
foreach(var item in _queue)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
2.Dequeue:
Dequeue() method is used to retrieve the top most element in a queue collection.
Dequeue() removes and returns a first element from a queue because the queue stores elements in FIFO order.
Calling Dequeue() method on empty queue will throw InvalidOperation exception. So always check that the total count of a queue is greater than zero before calling the Dequeue() method on a queue.
Example
class CollectionExamples
{
static void Main(string[] args)
{
Queue _queue = new Queue();
_queue.Enqueue(1);
_queue.Enqueue(2);
_queue.Enqueue(3);
_queue.Enqueue(3); //it allows duplicate
_queue.Enqueue(null); // it allows null
Console.WriteLine("Length Before Dequeue {0}", _queue.Count);
Console.WriteLine("Calling Dequeue {0}", _queue.Dequeue()); //Return first element and remove it.
Console.WriteLine("Length After Dequeue {0}", _queue.Count);
Console.ReadLine();
}
}
Output:
Length Before Dequeue 5
Calling Dequeue 1
Length After Dequeue 4
3. Peek() : The Peek() method always returns the first item from a queue collection without removing it from the queue.
Calling Peek() and Dequeue() methods on an empty queue collection will throw a run time exception "InvalidOperationException".
Example
class CollectionExamples
{
static void Main(string[] args)
{
Queue _queue = new Queue();
_queue.Enqueue(1);
_queue.Enqueue(2);
_queue.Enqueue(3);
_queue.Enqueue(3); //it allows duplicate
_queue.Enqueue(null); // it allows null
Console.WriteLine("Length Before Dequeue {0}", _queue.Count);
Console.WriteLine("Calling Dequeue {0}", _queue.Peek()); //Return first element
Console.WriteLine("Length After Dequeue {0}", _queue.Count);
Console.ReadLine();
}
}
Output
Length Before Dequeue 5
Calling Dequeue 1
Length After Dequeue 5
4.Contains()
The Contains() method checks whether an item exists in a queue. It returns true if the specified item exists; otherwise it returns false.
Example
class CollectionExamples
{
static void Main(string[] args)
{
Queue _queue = new Queue();
_queue.Enqueue(1);
_queue.Enqueue(2);
_queue.Enqueue(3);
_queue.Enqueue(3); //it allows duplicate
_queue.Enqueue(null); // it allows null
Console.WriteLine(_queue.Contains(1)); // it will return true
Console.ReadLine();
}
}
5.Clear() :The Clear() method removes all the items from a queue.
Example
static void Main(string[] args)
{
Queue _queue = new Queue();
_queue.Enqueue(1);
_queue.Enqueue(2);
_queue.Enqueue(3);
_queue.Enqueue(3); //it allows duplicate
_queue.Enqueue(null); // it allows null
_queue.Clear();
Console.WriteLine(_queue.Count);//it will return 0
Console.ReadLine();
}