Refresh hash table with another hash table?

How to update the values โ€‹โ€‹of one hash table with another hash table,

if the second hash table contains new keys, then they must be added to 1st else in order to update the value of the 1st hash table.

+6
collections hashtable c #
source share
2 answers
foreach (DictionaryEntry item in second) { first[item.Key] = item.Value; } 

If necessary, you can turn this into an extension method (provided that you are using .NET 3.5 or later).

 Hashtable one = GetHashtableFromSomewhere(); Hashtable two = GetAnotherHashtableFromSomewhere(); one.UpdateWith(two); // ... public static class HashtableExtensions { public static void UpdateWith(this Hashtable first, Hashtable second) { foreach (DictionaryEntry item in second) { first[item.Key] = item.Value; } } } 
+16
source share

Some code on this (based on a dictionary):

  foreach (KeyValuePair<String, String> pair in hashtable2) { if (hashtable1.ContainsKey(pair.Key)) { hashtable1[pair.Key] = pair.Value; } else { hashtable1.Add(pair.Key, pair.Value); } } 

I am sure there is a more elegant solution using LINQ (although, I have the code in 2.0;)).

0
source share

All Articles