[英]C# List AddRange - does it copy the items of add by reference
說,我有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節省內存,因為列表很大。
如果要復制引用而不是數據,則將整數列表包裝到如下所示的類中:
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
希望這是有用的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.