How to use C# Queue Class

The Queue class in C# is a built-in data structure that represents a first-in, first-out (FIFO) collection of objects. It is part of the System.Collections namespace and provides methods and properties for efficiently working with queues.

C# Queue class features:

  1. Adding Items: Items can be added to the queue using the Enqueue method. This method adds the specified object to the end of the queue, ensuring that it will be the last item to be dequeued.
  2. Removing Items: The Dequeue method is used to remove and return the object at the beginning of the queue. This method follows the FIFO principle, removing the item that was added first. It throws an exception if the queue is empty, so it is important to check the queue's Count property or use the TryDequeue method to safely dequeue items.
  3. Retrieving Items: The Peek method allows you to examine the object at the beginning of the queue without removing it. This method returns the object at the start of the queue, or throws an exception if the queue is empty.
  4. Counting Items: The Count property provides the number of items currently stored in the queue. This allows you to check the size of the queue and perform operations based on its count.

Important functions in the Queue Class

  1. Enqueue : Add an Item in Queue
  2. Dequeue : Remove the oldest item from Queue
  3. Peek : Get the reference of the oldest item

Enqueue : Add an Item in Queue

Syntax
Queue.Enqueue(Object)
  1. Object : The item to add in Queue
days.Enqueue("Sunday");

Dequeue : Remove the oldest item from Queue (we don't get the item later)

Syntax
Object Queue.Dequeue()
  1. Returns : Remove the oldest item and return.
days.Dequeue();

Peek : Get the reference of the oldest item (it is not removed permanently)

Syntax
Object Queue.Peek()
  1. returns : Get the reference of the oldest item in the Queue
days.peek();

The class Queue is a type-generic class in C# that may store objects for any type. Using generics in this approach helps you to create such a queue that can be strongly-typed, as only an object of a specified type will be able to enter the queue and not cast originally.

The Queue class in C# is typically used in scenarios where you need to process elements in the order they were added. It is commonly employed for implementing algorithms like breadth-first search, scheduling tasks, managing event queues, and more.

The following CSharp Source code shows some of commonly used functions :

Full Source C#
using System; using System.Collections; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Queue days = new Queue(); days.Enqueue("Sunday"); days.Enqueue("Monday"); days.Enqueue("Tuesday"); days.Enqueue("Wednsday"); days.Enqueue("Thursday"); days.Enqueue("Friday"); days.Enqueue("Saturday"); MessageBox.Show (days.Dequeue().ToString ()); if (days.Contains("Monday")) { MessageBox.Show("The queue contains Monday"); } else { MessageBox.Show("Does not match any entries"); } } } }

When you execute the above C# source code , you will get Sunday in the messagebox and then it check the Monday is exist in the queue or not.

Conclusion

The Queue class in C# provides a convenient and efficient way to work with a FIFO collection of objects. It allows you to add items to the end of the queue, remove items from the beginning, retrieve items without removal, and count the number of items in the queue. It is a versatile data structure that finds application in a wide range of scenarios, where order-based processing is required.