Topic : Queue Class - Generic Collection | Queue<T> Class in C# Programming Language.

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.



Queue : infobrother

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:


  • » Enqueue: Add an element to the end of the Queue.
    » Dequeue: Removes the Element from the start of the Queue.
    » Peek: Returns the Oldest element that is at the start of the Queue but does not remove it from the Queue.



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:


PropertyDescription
CountGets the Number of Elements contained in the Queue.


The following table show some of the commonly used Methods of the Queue Class:

MethodsDescription
Clear()Removes all objects from the Queue.
Contains()Determines whether an element is in the Queue.
EnqueueAdds 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.

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();
        }
    }
}


  • In Above Example:

    » Queue is Non-Generic Collection. so we can add elements of any data type into a Queue using the Enqueue() Method:
    » 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.
    » The Peek() method always returns the first item from a queue collection without removing it from the queue. Calling Peek() methods on an empty queue collection will throw a run time exception "InvalidOperationException".
    » The Contains() method checks whether an item exists in a queue. It returns true if the specified item exists; otherwise it returns false.
    » The Clear() method removes all the items from a queue.



For complete list of properties and methods, please visit Microsoft's C# documentation.















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement