07 Feb 2024




Intermediate

Queue Generic Collections in C#

Definition:

Queue<T> is a generic collection class in the System.Collections.Generic namespace of C#. It represents a first-in, first-out (FIFO) data structure, where elements are added at the end (enqueue) and removed from the beginning (dequeue). The T represents the type of elements stored in the queue.

Key Features and Characteristics:

  • FIFO Structure: Elements are added to the end of the queue, and removal occurs from the front. The first element added is the first to be removed.
  • Generic Type Parameter: Queue is generic, allowing it to store elements of any data type specified by the developer.
  • Dynamic Sizing: Similar to List, Queue can dynamically resize itself to accommodate more elements as needed.
  • Enqueue and Dequeue Operations: Elements are added to the queue using the Enqueue method and removed using the Dequeue method.
  • Peek Operation: The Peek method allows you to view the element at the front of the queue without removing it.
  • Count Property: The Count property provides the number of elements currently in the queue.
  • Clear Method: The Clear method removes all elements from the queue.

Uses:

  • Modeling scenarios where elements are processed in the order they arrive.
  • Implementing task scheduling systems or processing events in the order they occur.
  • Representing a print queue or any situation where the order of arrival is significant.

Advantages:

  • Efficient for scenarios where elements are processed in the order they are received.
  • Provides a straightforward way to manage a queue of items with built-in methods for enqueueing and dequeuing.
  • Dynamic resizing capability allows Queue to efficiently handle varying numbers of elements.

Disadvantages:

  • Not suitable for scenarios where random access to elements is required.
  • Insertion or removal of elements in the middle of the queue is not efficient.

Code Example:

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // Creating a Queue of strings
        Queue<string> messageQueue = new Queue<string>();

        // Enqueueing elements to the queue
        messageQueue.Enqueue("Message 1");
        messageQueue.Enqueue("Message 2");
        messageQueue.Enqueue("Message 3");

        // Dequeueing elements from the queue
        string dequeuedMessage = messageQueue.Dequeue();
        Console.WriteLine("Dequeued: " + dequeuedMessage); // Output: Dequeued: Message 1

        // Peek at the front element without dequeuing
        string peekedMessage = messageQueue.Peek();
        Console.WriteLine("Peeked: " + peekedMessage); // Output: Peeked: Message 2

        // Displaying the remaining elements in the queue
        Console.WriteLine("Remaining Elements in the Queue:");
        foreach (var message in messageQueue)
        {
            Console.WriteLine(message);
        }
    }
}

Explanation: This example demonstrates creating a Queue<string> and enqueueing three messages. The Enqueue method is used to add messages to the end of the queue. The Dequeue method removes the front element from the queue. The Peek method allows viewing the front element without removing it. Finally, a foreach loop is used to iterate over the remaining elements in the queue and print them.

c-sharp
queue
generic-collections