c# List添加List
时间: 2023-08-18 19:07:59 浏览: 316
在C#中,如果你想要将一个`List`添加到另一个`List`中,可以使用`AddRange`方法。下面是一个例子:
```csharp
List<int> list1 = new List<int>() { 1, 2, 3 };
List<int> list2 = new List<int>() { 4, 5, 6 };
list1.AddRange(list2);
```
在上面的例子中,我们创建了两个整型的`List`对象`list1`和`list2`,并分别添加了一些元素。然后,我们使用`AddRange`方法将`list2`中的所有元素添加到`list1`中。现在,`list1`中的元素为`{ 1, 2, 3, 4, 5, 6 }`。
注意,`AddRange`方法接受一个`IEnumerable`类型的参数,因此可以传递任何实现`IEnumerable`接口的集合类型,不仅仅是`List`类型。
阅读全文
相关推荐


















