C# | Get the number of elements contained in the Queue (original) (raw)
Last Updated : 01 Feb, 2019
Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Count Property is used to get the number of elements contained in the Queue.Properties:
- Enqueue adds an element to the end of the Queue.
- Dequeue removes the oldest element from the start of the Queue.
- Peek returns the oldest element that is at the start of the Queue but does not remove it from the Queue.
- The capacity of a Queue is the number of elements the Queue can hold.
- As elements are added to a Queue, the capacity is automatically increased as required by reallocating the internal array.
- Queue accepts null as a valid value for reference types and allows duplicate elements.
Syntax:
myQueue.Count
Here, myQueue is the name of the Queue.Return Value: This property returns the number of elements contained in the Queue.Example 1:
CSHARP `
// C# code to Get the number of // elements contained in the Queue using System; using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a Queue of strings
Queue<string> myQueue = new Queue<string>();
// Inserting the elements into the Queue
myQueue.Enqueue("Chandigarh");
myQueue.Enqueue("Delhi");
myQueue.Enqueue("Noida");
myQueue.Enqueue("Himachal");
myQueue.Enqueue("Punjab");
myQueue.Enqueue("Jammu");
// 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
Example 2:
CSHARP `
// C# code to Get the number of // elements contained in the Queue using System; using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a Queue of Integers
Queue<int> myQueue = new Queue<int>();
// 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
Reference:
Similar Reads
- C# | Get the number of elements contained in the Stack Stack represents a last-in, first out collection of object. Stack.Count Property is used to gets the number of elements contained in the Stack. Retrieving the value of this property is an O(1) operation. Syntax: myStack.Count Here myStack is the name of the Stack Return Value: The 2 min read
- C# | Get the number of elements contained in SortedList SortedList.Count Property is used to get the number of elements contained in a SortedList object. Syntax: public virtual int Count { get; } Property Value: The number of elements contained in the SortedList object. Below programs illustrate the use of above discussed method: Example 1: CSharp // C# 2 min read
- C# | Get the number of elements in the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Count Property is used to get the number of elements in the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elemen 2 min read
- C# | Get the number of elements actually contained in the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Count property gets the number of elements actually contained in 3 min read
- C# | Get the number of elements contained in Collection Collection.Count property is used to get the number of elements actually contained in the Collection. Syntax: public int Count { get; } Return Value: The number of elements actually contained in the Collection. Below given are some examples to understand the implementation 2 min read
- C# | Count the total number of elements in the List List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. L 2 min read
- C# | Get or set the number of elements that the ArrayList can contain ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Capacity property is used to get or set the number of elements th 2 min read
- Count the number of element present in the sequence in LINQ? In LINQ, you can count the total number of elements present in the given sequence by using the Count Method. This method returns the total number of elements present in the given sequence. This method can be overloaded in two different ways: Count(): This method returns the total numb 3 min read
- C# | Get the number of nodes contained in LinkedList LinkedList.Count property is used to get the number of nodes actually contained in the LinkedList. Syntax: public int Count { get; } Return Value: The number of nodes actually contained in the LinkedList. Note: Retrieving the value of this property is an O(1) operation. Below given 2 min read
- Copying the Queue elements to 1-D Array in C# Queue.CopyTo(T[], Int32) Method is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. The elements are copied to the Array in the same order in which the enumerator iterates through the Queue and this method is an O(n) operation, whe 4 min read