Queue.Count Property in C#

The Queue.Count property in C# is a read-only property that returns the number of elements currently stored in the Queue collection. This property provides an efficient way to check the size of the queue without needing to iterate through all elements.

Syntax

The syntax for the Queue.Count property is as follows −

public virtual int Count { get; }

Return Value

The Count property returns an int value representing the total number of elements in the Queue. If the queue is empty, it returns 0.

Using Count with Queue Operations

The following example demonstrates how the Count property changes as elements are added and removed from the queue −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue queue = new Queue();
      queue.Enqueue(100);
      queue.Enqueue(200);
      queue.Enqueue(300);
      queue.Enqueue(400);
      queue.Enqueue(500);
      queue.Enqueue(600);
      queue.Enqueue(700);
      queue.Enqueue(800);
      queue.Enqueue(900);
      queue.Enqueue(1000);
      Console.WriteLine("Queue...");
      foreach(int i in queue) {
         Console.WriteLine(i);
      }
      Console.WriteLine("Count of elements in the Queue = " + queue.Count);
      queue.Clear();
      Console.WriteLine("Count of elements in the Queue [After Clear] = " + queue.Count);
   }
}

The output of the above code is −

Queue...
100
200
300
400
500
600
700
800
900
1000
Count of elements in the Queue = 10
Count of elements in the Queue [After Clear] = 0

Count with Dynamic Queue Operations

This example shows how the count changes dynamically as elements are enqueued −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue queue = new Queue();
      queue.Enqueue(100);
      queue.Enqueue(200);
      queue.Enqueue(300);
      Console.WriteLine("Initial Queue...");
      foreach(int i in queue) {
         Console.WriteLine(i);
      }
      Console.WriteLine("Count of elements in the Queue = " + queue.Count);
      
      queue.Enqueue(800);
      queue.Enqueue(900);
      queue.Enqueue(1000);
      Console.WriteLine("Count after adding more elements = " + queue.Count);
      
      queue.Clear();
      Console.WriteLine("Count after clearing the Queue = " + queue.Count);
   }
}

The output of the above code is −

Initial Queue...
100
200
300
Count of elements in the Queue = 3
Count after adding more elements = 6
Count after clearing the Queue = 0

Count with Dequeue Operations

The following example demonstrates how Count decreases when elements are dequeued −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue queue = new Queue();
      queue.Enqueue("First");
      queue.Enqueue("Second");
      queue.Enqueue("Third");
      queue.Enqueue("Fourth");
      
      Console.WriteLine("Initial count: " + queue.Count);
      
      Console.WriteLine("Dequeuing: " + queue.Dequeue());
      Console.WriteLine("Count after first dequeue: " + queue.Count);
      
      Console.WriteLine("Dequeuing: " + queue.Dequeue());
      Console.WriteLine("Count after second dequeue: " + queue.Count);
      
      Console.WriteLine("Remaining elements: " + queue.Count);
   }
}

The output of the above code is −

Initial count: 4
Dequeuing: First
Count after first dequeue: 3
Dequeuing: Second
Count after second dequeue: 2
Remaining elements: 2

Conclusion

The Queue.Count property is essential for tracking the number of elements in a Queue collection. It automatically updates as elements are enqueued, dequeued, or when the queue is cleared, making it useful for queue management and conditional operations based on queue size.

Updated on: 2026-03-17T07:04:36+05:30

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements