2

I have four Dictionary , two are (dictionary within Dictionary), declaration shown below

    Dictionary<string, Dictionary<string, string>> dict_set = new Dictionary<string, Dictionary<string, string>>();
    Dictionary<string, Dictionary<string, string>> dict_Reset = new Dictionary<string, Dictionary<string, string>>();
    Dictionary<string, string> set_value = new Dictionary<string, string>();
    Dictionary<string, string> Reset_value = new Dictionary<string, string>();

I want to first add elements in dictionary set_vlaue and Reset_value. once the values are added then i am adding these dictionaries to other two dictionaries as shown below.

dict_set.Add(condiName, set_value);
dict_Reset.Add(condiName, Reset_value);
 set_value.Clear();
 Reset_value.Clear();

the values are getting added , but after adding set_value and reset_value dictionaries , i want to clear these two dictionaries set_value and reset_value,but problem occurs that when set_value and reset_value are cleared the data from dict_set and dict_reset is also cleared..

can any one help me , to how to create deep copy of dictionaries in this case...

Deadlock
  • 330
  • 1
  • 3
  • 21
  • 3
    See Jon Skeet's answer here http://stackoverflow.com/questions/139592/what-is-the-best-way-to-clone-deep-copy-a-net-generic-dictionarystring-t – keyboardP Jul 03 '13 at 08:56
  • @keyboardP actually i want to make a deep copy of dictionary while performing below operation.. dict_set.Add(condiName, set_value); dict_Reset.Add(condiName, Reset_value); one it is done i want to clear the set_value and reset_value.. so that new values can be added.. But as per your code if i make clone them before adding, one key will be having other key data.. – Deadlock Jul 03 '13 at 09:05
  • @KeyboardP: one problem with Skeet's answer is that it performs 'Clone' copy of leafs which does not guarantee deep copying. Author here wants a deep one, so that's not that exact-duplicate. Still with Skeet's answer, it is easy to glue up a solution. – quetzalcoatl Jul 03 '13 at 09:05
  • 2
    Also note that Skeet has _two_ answers in the thread linked in the first comment. – Jeppe Stig Nielsen Jul 03 '13 at 09:24

4 Answers4

6

I do not know what you are trying to do in the workflow, but why not to reinstancing instead of cleaning?

dict_set.Clear(); 

to:

dict_set = new Dictionary<string, string>(); 
Peter
  • 27,590
  • 8
  • 64
  • 84
mcj0in
  • 76
  • 1
  • actually set_value and reset_value dictionay are storing temp values inside loop one there job is over i want to clear them so that on next iteration these will be having fresh values.. – Deadlock Jul 03 '13 at 09:10
  • if i understand you correctly, you want to copy the values from set_/reset_value dictionaries to dict_set/_reset dictionaries, than clean up the set_/reset_value dictionaries. SO: dont use "set_/reset_value.Clear()" in your loop, use "set_/reset_value = new ..." – mcj0in Jul 03 '13 at 09:13
  • btw: you can change your add from dict_set.Add(condiName, set_value); dict_Reset.Add(condiName, Reset_value); to dict_set.Add(condiName, new Dictionary(set_value)); dict_Reset.Add(condiName, new Dictionary(Reset_value)); – mcj0in Jul 03 '13 at 09:19
  • Is it good idea to create new instance every time ?? It will create multiple instance in memory . – Rajeev Kumar Jul 03 '13 at 09:31
  • @mcjoin creating new instance everytime is solving my problem but donno it is a good solution or not.. – Deadlock Jul 03 '13 at 09:48
  • @Unknown You do need to have a new instance for every dictionary, dont't you? If i need put 6 eggs in my cake, is it a good idea to take 6 eggs out of the fridge? I think i have to... – Jeroen1984 Jul 03 '13 at 11:23
1

Aside of what Skeet has written in the answer pointed out by keyboardP, in most managed languages you may very easily perform deep copy by:

  • serializing the thing
  • deserializing it back

Upon deserialization, you'll usually have a complete deep clone of the original. The serializer will usually do all the ID-checking, breaking cycles, deduplicating, etc. In your case this is not necessary, but it may came handy later.

You may serialize it to XML, BinaryForm, JSON or whatever you like and have at hand. It is not that important.

Now back to your question:

This is your code, just shortened a bit:

var dict_set = new Dictionary<string, Dictionary<string, string>>();
var dict_Reset = new Dictionary<string, Dictionary<string, string>>();
var set_value = new Dictionary<string, string>();
var Reset_value = new Dictionary<string, string>();

dict_set.Add(condiName, set_value);
dict_Reset.Add(condiName, Reset_value);

set_value.Clear();
Reset_value.Clear();

You claim that:

(...) but problem occurs that when set_value and reset_value are cleared the data from dict_set and dict_reset is also cleared..

This is not true. With that code of above, it is not possible. set/reset/dict_set/dict_reset are 4 distinct objects. Calling "Clear" on "set/reset" cannot cause the others to be cleared.

Look at your code. The error is elsewhere. Not here. Something other is clearing that dict* dictionaries.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • after clearing the set_value.Clear(); Reset_value.Clear(); the data which was supposed to be in dict_set and dict_reset is missing.. only the content present is keyname i.e condiname, actually dict_set and dict_reset are dictionary with in dicitionary.. – Deadlock Jul 03 '13 at 09:32
0

By creating new instance will solve the above issue.. if any one came across some other better solution please post..

     dict_set.Add(condiName, new Dictionary<string, string>(set_value));
     dict_Reset.Add(condiName, new Dictionary<string, string>(Reset_value)); 
     set_value.Clear();
     Reset_value.Clear();
Deadlock
  • 330
  • 1
  • 3
  • 21
0

I think this way the inner dictionaries in dict_set and dict_Reset will not be cleared

dict_set.Add(condiName, set_value.ToDictionary(entry => entry.Key,entry => entry.Value));
dict_Reset.Add(condiName, Reset_value.ToDictionary(entry => entry.Key,entry => entry.Value));
set_value.Clear();
Reset_value.Clear();

With the ToDictionary() method you actually create a new Dictionary object, not using the reference to tour originals dictionaries anymore, so you can safely clear them. Adding and removing data to the set_value and Reset_value alsdo does not affect the dictionaries inside dict_set and dict_Reset

Just out of curiousity, why Reset_value with a capital S and set_value not?

Jeroen1984
  • 1,616
  • 1
  • 19
  • 32