單純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);