C#队列 - 如何将结构添加到每个队列点
问题描述:
C#队列和每个位置都有一个整数和一个字符串的集合?我知道如何创建一个整数或字符串队列,但是如何创建一个在每个队列点都有多个数据类型的队列?C#队列 - 如何将结构添加到每个队列点
struct ABC { int val1; int val2; }
static void Main(string[] args) {
System.Collections.Generic.Queue<ABC> queue = new System.Collections.Generic.Queue<ABC>();
queue.Enqueue(ABC);
// ...
}
答
您需要创建一个结构实例,为其赋值,然后对其进行排队。第二个问题是你没有使用公共字段/属性。
struct ABC { public int val1; public int val2; }
static void Main(string[] args) {
System.Collections.Generic.Queue<ABC> queue = new System.Collections.Generic.Queue<ABC>();
ABC queuable = new ABC() { val1 = 3, val2 = 39};
queue.Enqueue(queuable);
// ...
}
当然,你也可以添加using
报表导入System.Collections.Generic
命名空间来清理一下代码了。
创建一个符合您需求的对象,并使用泛型。 '队列'https://msdn.microsoft.com/en-us/library/7977ey2c(v=vs.110).aspx –
ps2goat
这就是我的: – Unity
struct ABC { int val1; int val2; } 静态无效的主要(字符串[]参数) { System.Collections.Generic.Queue队列=新System.Collections.Generic.Queue (); queue.Enqueue(ABC); –
Unity