【问题标题】:C# queue - how to add structure to each queue spotC# 队列 - 如何将结构添加到每个队列点
【发布时间】:2017-02-03 21:43:56
【问题描述】:

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);
 // ...
}

不确定如何将 ABC 添加到队列中,或者如何将整数放入 val1 和 val2。

【问题讨论】:

    标签: c#-4.0


    【解决方案1】:

    您需要创建该结构的一个实例,为其赋值,然后对其进行排队。第二个问题是您没有使用公共字段/属性。

    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 命名空间来清理代码。

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多