[No000086]C#foreach集合被改变,报错处理方案

C#集合与字典操作示例
本文通过示例展示了C#中List与Dictionary的基本使用方法,包括元素的添加、遍历及移除等常见操作,并探讨了在迭代过程中修改集合可能引发的问题。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace ConsoleTest
{

    class Program
    {
        class Test
        {
            public string Num = "";
        }

        static void Main(string[] args)
        {

            List<Test> listTest = new List<Test>();

            for (int i = 0; i < 10; i++)
            {
                listTest.Add(new Test { Num = i.ToString() });
            }
            int NumCount = 10;
            //foreach (var list in listTest)
            //{
            //    NumCount++;
            //    if (listTest.Count < 20)
            //    {
            //        listTest.Add(new Test { Num = NumCount.ToString() });
            //    }
            //    Console.WriteLine(list.Num);
            //}//报错
            NumCount = 100;
            for (int i = 0; i < listTest.Count; i++)
            {
                NumCount++;
                if (listTest.Count < 20)
                {
                    listTest.Add(new Test { Num = NumCount.ToString() });
                }
                Console.WriteLine(listTest[i].Num);
            }

            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("1", "helo1");
            dict.Add("2", "helo2");
            var keysList = new string[dict.Count];
            dict.Keys.CopyTo(keysList, 0);
            foreach (var key in keysList)
            {
                Console.WriteLine("key_  " + key.ToString() + ":" + dict[key]);
                dict.Remove(key);          //   Response.Write("key" + key.ToString() + ":" + dict[key]);   
            }
            Console.WriteLine("AfterRemoved!=========================================");

            foreach (var dic in dict)
            {
                Console.WriteLine(dic.Key, dic.Value);
            }
            Console.WriteLine("dic AfterShow!=========================================");

            Dictionary<int, string> dict2 = new Dictionary<int, string>();
            dict2.Add(1, "helo21");
            dict2.Add(2, "helo22");
            var keysList2 = new int[dict2.Count];
            dict2.Keys.CopyTo(keysList2, 0);
            foreach (var key2 in keysList2)
            {
                Console.WriteLine("key_  " + key2.ToString() + ":" + dict2[key2]);
                dict.Remove(key2.ToString());          //   Response.Write("key" + key.ToString() + ":" + dict[key]);   
            }
            Console.WriteLine("dict2 AfterRemoved!=========================================");

            foreach (var dic in dict)
            {
                Console.WriteLine(dic.Key, dic.Value);
            }


            Console.ReadLine();

            Console.Read();


        }
    }
}

 

转载于:https://www.cnblogs.com/Chary/p/No000086.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值