Để biết số phần tử trong Stack, dùng thuộc tính Count.
Ví dụ
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);
}
}
}
Kết quả
Count: 8
Lưu ý: Count trả về số phần tử hiện tại. Dùng Peek() để xem phần tử đỉnh mà không xóa, Pop() để lấy và xóa.