清单和清除方法存在问题:

问题描述

|
Dictionary<string,List<DisplayAllQuestionsTable>> tPages= new Dictionary<string,List<DisplayAllQuestionsTable>>();;
List<DisplayAllQuestionsTable> threadsByTopic = new List<DisplayAllQuestionsTable>();
int tPage=0;
        foreach (var topicKeys in postsByTopic)
        {

               if (topicKeys.Key == subTopic)
                {
                    foreach (var item in postsByTopic[topicKeys.Key])
                    {
                        questionNumber++;
                        maximumTopicPages++;
                        threadsByTopic.Add(item);//Adds All DisplayAllTables objects
                        //if there are 20 add a button.
                        if (maximumTopicPages == 20)
                        {
                            tPages.Add(tPage.ToString(),threadsByTopic);//The threadsByTopic clears everytime i call threadsByTopic.clear()

                            threadsByTopic.Clear();
                            tPage++;

                    }
                }
我知道,如果它是一个引用,则它的引用是按值传递的。但是,如果我将其threadByTopic列表添加到字典中,是否按原样保存...?还是必须重置?     

解决方法

        该实例已添加到字典中,但其列表仍然相同,因此当您使用clear时,会清除您刚刚添加的列表。 U应该创建新的列表对象,而不要使用clear。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...