类型字典清除问题字典 [英] Dictionary of type dictinary clearing problem

查看:95
本文介绍了类型字典清除问题字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用dictinary类型的字典.我正在循环读取一些数据.
第一次填写dictianry 1和dictianry 2时一切都很好.
接下来,我必须更改dictianry中的数据并将其添加到dictianry 1中.
为此,我正在清除dictianry2.

当我执行此操作时,以前的数据也会更改.

因为字典是参考类型.

我需要解决这个问题.

有任何想法吗?

Hi
I am using dictionary of type dictinary. and i am reading in loop some data.
first time when i fill dictianry 1 and dictianry 2 all is fine.
next i have to change data in the dictianry and add it to dictianry 1.
for this I am clearing dictianry 2.

and when i do this operation the previous data also get changed.

As dictianry is of reference type.

I need work around for this.

Anyone ahve any idea?

推荐答案

您需要进行深层复制.请查看以下链接以获取更多信息:
http://msdn.microsoft.com/en-us/library/system.object. memberwiseclone.aspx [ ^ ]

在C#中对对象进行深度复制 [
You need to do a deep copy. Have a look at the following links for more info:
http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx[^]

Deep copy of objects in C#[^]

Good luck!


这是由于您复制数据的方式所致.您实质上是在对同一对象添加其他引用,因此,当您在一个词典中对其进行更改时,在另一个词典中也会对其进行更改.
It''s due to the way you''re copying the data. You''re essentially adding an additional reference to the same object, so when you change it in one dictionary, it also changes in the other.


这篇关于类型字典清除问题字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆