用空列表初始化列表数组的最有效方法是什么

本文关键字:列表 有效 方法 是什么 数组 初始化 | 更新日期: 2023-09-27 18:22:12

我想以最有效的方式初始化一个带有空List的List数组。

正确的方法是什么?

    List<int>[] neighbour = new List<int>[10]; // this does not create the List
    for (int i = 0 ; i < neighbour.Length ; ++i)
        neighbour[i] = new List<int>();        // is that really efficient ? 

用空列表初始化列表数组的最有效方法是什么

试试这个:

var neighbour = Enumerable.Range(0, 10).Select(x=> new List<int>()).ToArray();