List<T>.AddRange(IEnumerable<T>)
方法可以将一组元素添加到列表的末尾:
myList.AddRange(moreItems); // Adds moreItems to the end of myList
如何将一组项(如IEnumerable<T>
)添加到列表的开头是最有效的方法?
List<T>.AddRange(IEnumerable<T>)
方法可以将一组元素添加到列表的末尾:
myList.AddRange(moreItems); // Adds moreItems to the end of myList
如何将一组项(如IEnumerable<T>
)添加到列表的开头是最有效的方法?
使用InsertRange方法:
List<T>.InsertRange(0, yourcollection);
同时查看插入方法,可以在指定索引处向列表中添加元素。
将一个元素插入到列表中指定的索引位置。
List<T>.Insert(0, T);
List<String> listA=new List<String>{"A","B","C"};
List<String> listB=new List<String>{"p","Q","R"};
listA.InsertRange(0, listB);
在这里,假设我们有两个字符串列表...然后使用InsertRange方法,我们可以传递想要将新范围(listB)插入/推入现有范围(listA)的起始索引。
希望这样可以使代码更加清晰易懂。
List<T>.InsertRange(0, IEnumerable<T>)
。