List和clear方法存在问题:

时间:2011-06-15 18:23:16

标签: c# list dictionary

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列表添加到字典..它保存原样...?还是必须重置?

1 个答案:

答案 0 :(得分:1)

实例被添加到字典中,但它仍然是相同的列表,因此当你使用clear u时,清除你刚刚添加的列表。你应该使用clear来创建新的列表对象。