public static void TestRemove() { string[] str = { "1", "2", "d", "x" }; Listlist = new List (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 (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:0dResult:1:0xResult:2:1============================================xResult:3:1dResult:2:1 delete .index:2 value:d2Result:1:01Result:0:0 delete .index:0 value:1