for和foreach 的区别

List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };

            //这里用foreach时,会出错,主要是foreach在检索的时候,它的数组不能变 for循环的就可以

            //foreach (int i in list)

            //{

            //    MessageBox.Show(i.ToString());

            //    list.Remove(i);

            //}

            for (int j = 0; j < list.Count; j++)

            {

                MessageBox.Show(list[j].ToString());

                list.Remove(list[j]);

                j--;

            }

原文地址:https://www.cnblogs.com/wenming205/p/1901453.html