温馨提示×

C# List的赋值问题的解决

c#
小云
424
2023-08-15 14:39:00
栏目: 编程语言

在C#中,List对象的赋值问题可以通过以下几种方式解决:

  1. 使用构造函数进行赋值:
List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int>(list1);
  1. 使用List的AddRange方法将一个List的元素添加到另一个List中:
List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int>();
list2.AddRange(list1);
  1. 使用List的ToList方法将一个List对象转换为另一个List对象:
List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = list1.ToList();

无论使用哪种方式,都可以实现将一个List对象赋值给另一个List对象。

0