2012-02-05 131 views
0

我,我會非常想表現爲這樣的數據:結構在C#和構造

LinkedList<T>[] 

但是,你不能這樣做仿製藥,所以我把它包在一個結構:

public struct SuitList      
    { 
     LinkedList<T> aList; 

     public SuitList() 
     { 
      aList = new LinkedList<T>(); 
     } 
    } 
現在

,在我的課我有

SuitList[] myStructList;  //there is only 4 indices of myStructList 

如何初始化ALIST,我的構造函數的類裏面?我想這個如下:

myStructList = new SuitList[4]; 

for(int i = 0; i < 4; i++) 
{ 
    myStructList[i] = new SuitList(); 
} 

編譯器給了我一個錯誤說的Structs不能包含明確的參數構造函數。有沒有更好的方法來做到這一點? 感謝您的幫助提前。

+2

這聽起來像一個很奇怪的構造。你爲什麼想要一個鏈表? – SLaks 2012-02-05 23:38:55

+1

當您嘗試使用「LinkedList []'時收到了什麼錯誤?它沒有什麼問題。 – phoog 2012-02-05 23:44:50

+0

@phoog,你是對的。我錯了 – CodeKingPlusPlus 2012-02-06 00:36:52

回答

7

C#不是Java。
你可以用泛型來做到這一點。

要回答你的問題,你應該創建一個類,而不是一個結構。

0

你可以做到這一點,而不必成爲一個類。

public struct SuitList<T> 
    { 
     LinkedList<T> aList; 

     public SuitList(int nothing = 0) 
     { 
      aList = new LinkedList<T>(); 
     } 
    }