2017-02-03 58 views
0

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

創建一個符合您需求的對象,並使用泛型。 '隊列'https://msdn.microsoft.com/en-us/library/7977ey2c(v=vs.110).aspx – ps2goat

+0

這就是我的: – Unity

+0

struct ABC { int val1; int val2; } 靜態無效的主要(字符串[]參數) { System.Collections.Generic.Queue 隊列=新System.Collections.Generic.Queue (); queue.Enqueue(ABC); – Unity

回答

0

您需要創建一個結構實例,爲其賦值,然後對其進行排隊。第二個問題是你沒有使用公共字段/屬性。

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命名空間來清理一下代碼了。