यह पता लगाने के लिए कि स्टैक क्लास में कितने तत्व जोड़े गए हैं, आपको काउंट प्रॉपर्टी का उपयोग करने की आवश्यकता है।
आइए पहले स्टैक में तत्वों को जोड़ें -
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); आइए देखें पूरा कोड -
उदाहरण
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);
}
}
} आउटपुट
Count: 8