C#:堆栈有两种类型的项目

标签 c#

我想用两种不同的类型制作堆栈

所以我试着写这个但是报错:

Stack<[string,int]> S = new Stack<[string,int]>();
S.Push(["aaa",0]);

我以前试过这种方式:

public class SItem{
    public string x;
    public int y;

    public SItem(string text,int index){
        this.x = text;
        this.y = index;
    }
}
Stack<SItem> S = new Stack<SItem>();
SItem Start = new SItem("aaa",0);
S.Push(Start);

但我希望它像我之前写的那样使用起来非常简单

有什么想法吗?

最佳答案

可以考虑Tuple :

var stack = new Stack<Tuple<string, int>>();
stack.Push(Tuple.Create("string", 0));
var item = stack.Pop();

它消除了编写自定义类的需要,但缺点是,正如 @AlexeiLevenkov 评论的那样,代码的可读性较差。

关于C#:堆栈有两种类型的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30807876/

相关文章:

c# - 在基于网格的游戏中放置墙壁/道路

C#:如何将文本附加到类中表单上的文本框?

c# - 活体瓷砖的正确使用方法?

c# - c# wpf 动画结束时运行 Action

c# - win CE 6.0 设备上的按钮

c# - 循环 ienumerable 并获取值

C# 将 YYYY-MM-DD 转换为 MM/DD/YYYY ("-"TO "/")

C# 如何在命令行应用程序中使用异步方法运行线程

c# - 带有适配器和组合框的 DataGridView

c# - 您会使用哪种 C# 项目类型重新开发 MFC C++ activex 控件?