开发者

A problem with List and clear method:

开发者 https://www.devze.com 2023-03-13 17:38 出处:网络
Dictionary<string, List<DisplayAllQuestionsTable>> tPages= new Dictionary<string, List<DisplayAllQuestionsTable>>();;
Dictionary<string, List<DisplayAllQuestionsTable>> tPages= new Dictionary<string, List<DisplayAllQuestionsTable>>();;
List<DisplayAllQuestionsTable> threadsByTopic = new List<DisplayAllQuestionsTable>();
int tPage=0;
        foreach (var topicKeys in postsByTopic)
        {

     开发者_StackOverflow          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++;

                    }
                }

I know that if it is a reference, its reference is passed by value. But if i add it the threadByTopic list to the dictionary..isnt it saved as it is...? or does it have to be reset?


The instance is added to dictionary, but its still the same list so when u use clear u are clearing the list u just added. U should create new list object instead using clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消