将一个泛型集合中的数据赋给另一个泛型集合

单纯new一个用“=”的话 改变新new的 原数组也会随之改变 是跟随的。

因此要单纯赋值的话有一个简单的方法:

// 如果真要这么做,但是有不想麻烦的话,就直接使用 ToList 方法,
List<int> a = new List<int> { 1, 2, 3, 4 };
List<int> b = a.ToList();
a.Clear();
Console.WriteLine(b.Count);

 或者:

  List<int> a = new List<int>() { 1, 2, 6, 7 };
  List<int> b = new List<int>(a);

 

posted @ 2017-01-04 09:08  苦力劳动者  阅读(2307)  评论(0编辑  收藏  举报