IEnumerable 集合迴圈賦值會不成功!要改為 List 集合迴圈賦值
阿新 • • 發佈:2018-11-09
public static void Smo2(ref IEnumerable<AA> list) { foreach (var item in list) { Console.WriteLine(item.Price); } foreach (var item in list) { if (item.Price == "0") { item.Price = "3";//這裡賦值會不成功 continue; } } foreach (var item in list) { Console.WriteLine(item.Price); } } public static void Smo(ref List<AA> list) { foreach (var item in list) { Console.WriteLine(item.Price); } foreach (var item in list) { if (item.Price == "0") { item.Price = "3";//這裡可以賦值成功 continue; } } foreach (var item in list) { Console.WriteLine(item.Price); } }