2012-11-27 45 views
1

C#中初始化列表清單是否有一個簡潔的語法?初始化列表清單的簡潔語法

我試圖

new List<List<int>>{ 
    {1,2,3}, 
    {4,5}, 
    {6,7,8,9} 
}; 

但我得到一個錯誤 '沒有超載的方法 '添加' 3個參數'


編輯:我知道長語法

new List<List<int>>{ 
    new List<int>   {1,2,3}, 
    new List<int>   {4,5}, 
    new List<int>   {6,7,8,9} 
}; 

我只是在尋找一些事情。

+0

可能的複製問題:http://stackoverflow.com/questions/665299/are-2-dimensional-lists-possible-in-c – 2012-11-27 14:28:50

+0

不是一個答案,但如果你使用的是數組,你可以這樣做:'new List {new [] {1,2,3},new [] {4,5,6}}; –

回答

6

不,你需要爲每個new List<int>

var lists = new List<List<int>>() { 
    new List<int>{1,2,3}, 
    new List<int>{4,5}, 
    new List<int>{6,7,8,9} 
};