Queue.Count Property in C#

This property is used to get the number of elements contained in the Queue. Retrieving the value of this property is an O(1) operation and it comes under the System.Collections namespace.

Syntax:

public virtual int Count { get; }

Property Value: This property returns the number of elements contained in the Queue.

Below programs illustrate the use of the above-discussed property:

Example 1:




// C# code to illustrate the 
// Queue.Count Property
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements"+
                         " in the Queue are : ");
  
        // The function should return 0
        // as the Queue is empty and it
        // doesn't contain any element
        Console.WriteLine(myQueue.Count);
    }
}


Output:

Total number of elements in the Queue are : 0

Example 2:




// C# code to illustrate the 
// Queue.Count Property
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue myQueue = new Queue();
  
        // Inserting the elements into the Queue
        myQueue.Enqueue("C");
        myQueue.Enqueue("C++");
        myQueue.Enqueue("Java");
        myQueue.Enqueue("C#");
        myQueue.Enqueue("HTML");
        myQueue.Enqueue("CSS");
  
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements "+
                           "in the Queue are : ");
  
        Console.WriteLine(myQueue.Count);
    }
}


Output:

Total number of elements in the Queue are : 6

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.queue.count?view=netframework-4.7.2


Contact Us