C# - How to Count Total Items/Elements in a Queue?

Learn, how to count total items/elements of Queue using Count Property of Queue class? By IncludeHelp Last updated : March 28, 2023

Count Total Items/Elements in a Queue

To count total items/elements in a queue, we use Queue.Count property. This property returns the total number of elements of a Queue.

Read more: C# Queue.Count property

C# program to count total items/elements in a queue

using System;
using System.Collections;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      Queue Q = new Queue(5);

      Q.Enqueue(10);
      Q.Enqueue(20);
      Q.Enqueue(30);
      Q.Enqueue(40);

      Console.WriteLine("Total items into queue: " + Q.Count);
    }
  }
}

Output

Total items into queue: 4

Explanation

In this program, we are inserting 4 items into Queue, and then counting total items using Count property of Queue class.

Note: In above program, to use 'Queue' class, we need to include System.Collection namespace.

C# Data Structure Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.