Get the number of elements contained in the Stack in C#

The Count property in C# provides an efficient way to get the number of elements contained in a Stack<T>. This property returns an integer value representing the current number of items stored in the stack.

Syntax

Following is the syntax for using the Count property −

Stack<T> stack = new Stack<T>();
int count = stack.Count;

Return Value

The Count property returns an int value representing the number of elements in the stack. For an empty stack, it returns 0.

Stack Count Property H G F E ... Stack Count stack.Count = 8

Using Stack Count Property

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      Stack<string> stack = new Stack<string>();
      stack.Push("A");
      stack.Push("B");
      stack.Push("C");
      stack.Push("D");
      stack.Push("E");
      stack.Push("F");
      stack.Push("G");
      stack.Push("H");
      Console.WriteLine("Count of elements = "+stack.Count);
      Console.WriteLine("Elements in Stack...");
      foreach (string res in stack){
         Console.WriteLine(res);
      }
      stack.Clear();
      Console.Write("Count of elements (updated) = "+stack.Count);
   }
}

The output of the above code is −

Count of elements = 8
Elements in Stack...
H
G
F
E
D
C
B
A
Count of elements (updated) = 0

Using Count with Different Data Types

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      Stack<int> stack = new Stack<int>();
      stack.Push(10);
      stack.Push(20);
      stack.Push(30);
      stack.Push(40);
      stack.Push(50);
      stack.Push(60);
      stack.Push(70);
      stack.Push(80);
      stack.Push(90);
      stack.Push(100);
      Console.WriteLine("Count of elements = "+stack.Count);
      Console.WriteLine("Elements in Stack...");
      foreach (int res in stack){
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Count of elements = 10
Elements in Stack...
100
90
80
70
60
50
40
30
20
10

Dynamic Count Monitoring

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      Stack<string> stack = new Stack<string>();
      Console.WriteLine("Initial count: " + stack.Count);
      
      stack.Push("First");
      Console.WriteLine("After Push: " + stack.Count);
      
      stack.Push("Second");
      stack.Push("Third");
      Console.WriteLine("After adding 2 more: " + stack.Count);
      
      stack.Pop();
      Console.WriteLine("After Pop: " + stack.Count);
      
      stack.Clear();
      Console.WriteLine("After Clear: " + stack.Count);
   }
}

The output of the above code is −

Initial count: 0
After Push: 1
After adding 2 more: 3
After Pop: 2
After Clear: 0

Conclusion

The Count property provides an O(1) time complexity operation to retrieve the number of elements in a Stack<T>. It dynamically updates as elements are pushed, popped, or when the stack is cleared, making it useful for monitoring stack size and implementing conditional logic based on stack contents.

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

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements