Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update hashtable by another hashtable?

How can I update the values of one hashtable by another hashtable,

if second hashtable contains new keys then they must be added to 1st else should update the value of 1st hashtable.

like image 989
shahjapan Avatar asked Nov 30 '09 14:11

shahjapan


1 Answers

foreach (DictionaryEntry item in second)
{
    first[item.Key] = item.Value;
}

If required you could roll this into an extension method (assuming that you're using .NET 3.5 or newer).

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;
        }
    }
}
like image 182
LukeH Avatar answered Nov 09 '22 05:11

LukeH