3

说,我有 3 个列表

List<int> l1
List<int> l1,l2,l3

所有 3 个列表都有很多项目我想将它们全部添加到一个列表中

List<int> finalList
finalList.AddRange(l1) , similarly for l2 and l3.

这样做finalList.AddRange时,它会复制 l1、l2、l3 中的项目还是仅引用这些项目?如果它复制我想避免 AddRange 以节省内存,因为列表很大。

4

1 回答 1

0

如果您希望复制引用而不是数据,请将您的整数列表包装到一个类中,如下所示:

    public class ItemsList
    {
        public List<int> ListOfInts {get; set;}

        public ItemsList()
        {
            ListOfInts = new List<int>();
        }
    }

然后像下面这样添加它们:

        ItemsList l1 = new ItemsList();
        l1.ListOfInts = new List<int> { 1, 2, 3, 4, 5, 6 };//or whatever data inside

        //same for l2, l3

        List<ItemsList> finalList = new List<ItemsList>();
        finalList.Add(l1);//Your adding references to ItemsList class 

希望这很有用。

于 2017-02-02T06:43:25.697 回答