C# 中的 Stack 类是什么?
csharpprogrammingserver side programming更新于 2025/10/13 17:52:17
当需要后进先出访问元素时,可以使用 Stack。向列表中添加元素称为"入栈",移除元素称为"出栈"。
让我们看一个 C# 中 Stack 类的示例 −
首先,在 Stack 中添加元素。
Stack st = new Stack();
st.Push('H');
st.Push('I');
st.Push('J');
st.Push('K');
st.Push('L');
现在计算 Stack 中元素的数量 −
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');
Console.WriteLine("Count: "+st.Count);
}
}
}
输出
Count: 5

