将List <int>转换为List <List <int >>

Myk*_*huk 5 .net c# collections list

我能转换List<int>成 List<List<int>>c#吗?当我使用这样的结构

List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>>(element);
Run Code Online (Sandbox Code Playgroud)

我弄错了,但我可以用另一种方式做到这一点吗?

Nol*_*nar 12

你可以这样做:

List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);
Run Code Online (Sandbox Code Playgroud)


Po-*_*toe 8

List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>> { element };
Run Code Online (Sandbox Code Playgroud)

那会有用.您无法在构造函数中传递列表.


Cyr*_*don 7

试试这个 :

var element = new List<int>();
var superElement = new List< List<int> >(){ element };
Run Code Online (Sandbox Code Playgroud)


Dar*_*ren 5

是.您可以使用集合初始值设定项

List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>> { element };
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-gb/library/vstudio/bb384062.aspx