我想用两种不同的类型制作堆栈
所以我试着写这个但是报错:
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/