列表>>添加;当我清除TempLst时,MasterLst也将清除 [英] List>> add; When i clear the TempLst the MasterLst also get clear
问题描述
在我的代码中,我正在使用MasterLst of List<keyvaluepair><int,>>>
类型的列表.
当我添加TempLst of List<timespansent> type to MasterLst
时,然后当我清除TempLst时,MasterLst也将清除.
我如何避免这种情况
代码:
静态列表< keyvaluepair>< int,>>> Masterlst = 新 List< keyvaluepair>< int,>>>();
列表×pansent> lsttemp = 新列表< timespansent>();
for ( int i = 0 ; i < lstday.Count; i ++)
{
lsttemp.Add(lstday [i]);
}
Masterlst.Add(新 KeyValuePair< int,>>(DayID,lsttemp));
lsttemp.Clear();
请帮我...
在此先感谢.与lsttemps单独列表声明相同.
如果您想跟踪masterlst中的列表,则需要将其放置在masterlst中的第二个列表.
但是,如果我正确理解了您的示例,则您只想清除lsttemps,因为您已将其添加到masterlst中,不是吗?
如果是这样,只是不清除lsttemp,而是将其值设置为null,那么剩下的唯一参考管理者是masterlst,当您从主列表中删除列表时,您可以决定清除它,或者如果您根本不清除它希望垃圾收集器将其全部处理.
我希望我提供了足够的信息.
In my code i am using MasterLst of List<keyvaluepair><int,>>>
type of list.
When i add a TempLst of List<timespansent> type to MasterLst
and then when i Clear the TempLst the MasterLst Also get Clear.
How can i Avoid this
Code:
static List<keyvaluepair><int,>>> Masterlst = new List<keyvaluepair><int,>>>();
List<timespansent> lsttemp = new List<timespansent>();
for (int i = 0; i < lstday.Count; i++)
{
lsttemp.Add(lstday[i]);
}
Masterlst.Add(new KeyValuePair<int,>>(DayID, lsttemp));
lsttemp.Clear();
Please Help me...
Thanks in Advance....
The problem u''ve is that lsttemp it''s reference is added to Masterlst, in other words the Masterlst lsttemps version is the same as lsttemps seperate list declaration.
If u want to keep track of the list in masterlst u''ll need a 2nd list which u place in masterlst.
But if i understand your example correctly u only want to clear lsttemps cause u''ve added it to the masterlst, isn''t it?
If so just don''t clear lsttemp set it''s value to null instead, so the only reference keeper remaining is masterlst, when u remove the list from the masterlist u could decide to clear it then, or not at all if u want the garbagecollector to take care of it all.
I hope i''ve supplied enough information.
这篇关于列表>>添加;当我清除TempLst时,MasterLst也将清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!