What is the Count property of Stack class in C#?


To find how many elements are added in the Stack class, you need to use the Count property.

Let us first add elements in the Stack −

Stack st = new Stack();

st.Push('H');
st.Push('I');
st.Push('J');
st.Push('K');
st.Push('L');
st.Push('M');
st.Push('N');
st.Push('O');

Now count the number of elements in the Stack −

Console.WriteLine("Count: "+st.Count);

Let us see the complete code −

Example

 Live Demo

using System;
using System.Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         Stack st = new Stack();

         st.Push('H');
         st.Push('I');
         st.Push('J');
         st.Push('K');
         st.Push('L');
         st.Push('M');
         st.Push('N');
         st.Push('O');

         Console.WriteLine("Count: "+st.Count);
      }
   }
}

Output

Count: 8

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements