C#中的Stack类是什么?
当您需要对项目进行后进先出的访问时,可以使用堆栈。将项目添加到列表时,称为推送项目,将其删除时,称为弹出项目。
让我们看一下C#中的堆栈类的示例-
首先,在堆栈中添加元素。
Stack st = new Stack();
st.Push('H');
st.Push('I');
st.Push('J');
st.Push('K');
st.Push('L');现在计算堆栈中的元素数-
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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短