Unity 使用C#List (Clear())出现的问题

2021-03-11 11:05发布

C# List 列表在开发中遇到的问题… 真的是很无语


正常使用List 的Clear() 理论上讲会把列表清空,,,

我遇到的问题就是清不空…而且不止一次的遇到,,,不知道到底是哪里出现的问题,,,


解决方法:在需要用Clear() 的时候 重新new一下就没问题了,,,


问题实例:


            //定义...

            public Dictionary<int, List<int>> NowOutCards = new Dictionary<int, List<int>>();

           

            //使用

            //int[] outcard = JsonMapper.ToObject<int[]>(((JsonData)evt.data)["card"].ToJson());

            int[] outcard = new []{1,2};

            List<int> tempList = new List<int>();

            for (int i = 0; i < outcard.Length; i++)

            {

                tempList.Add(outcard[i]);

            }


            tempList.Clear();


            tempList.AddRange(tempList);

            //记录...

            if (model.NowOutCards.ContainsKey(0))

            {

                //model.NowOutCards[0] .Clear();    //偶尔会有问题...

                model.NowOutCards[0] = new List<int>(); //解决问题...

                model.NowOutCards[0].AddRange(tempList);

            }

            else

            {

                model.NowOutCards.Add(0, tempList);

            }


不是使用字典(Dictionary)套用List 出的问题,,,之前单单使用List 也出现过这个问题,,,

也不是必出的问题,,,之前用一直好用,,,也许是后期做了哪些操作对其有影响了,,,然后就会偶尔不好用,,,还不知道具体哪里有问题,,若有知道的大佬,望您不吝赐教。



————————————————

版权声明:本文为CSDN博主「妳是我改卟了的bug」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/Czhenya/article/details/84328625