public static void TestRemove() { string[] str = { "1", "2", "d", "x" }; List<string> list = new List<string>(str); #region has error for (int i = 0; i < list.Count; i++) { Console.WriteLine(list[i]); Console.WriteLine("Result:" + i + ":" + i / 2); if (i % 2 == 0) { list.Remove(list[i]); } } #endregion Console.WriteLine("============================================"); list = new List<string>(str); for (int i = list.Count-1; i >= 0; i--) { Console.WriteLine(list[i]); Console.WriteLine("Result:" + i + ":" + i / 2); if (i % 2 == 0) { Console.WriteLine(string.Format(" delete .index:{0} value:{1}",i,list[i])); list.Remove(list[i]); } } }
output:
1
Result:0:0
d
Result:1:0
x
Result:2:1
============================================
x
Result:3:1
d
Result:2:1
delete .index:2 value:d
2
Result:1:0
1
Result:0:0
delete .index:0 value:1