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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Total number of elements in the Queue are : 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Total number of elements in the Queue are : 6

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.