views:

120

answers:

2

Given the following code:

Hashtable main = new Hashtable();

Hashtable inner = new Hashtable();
ArrayList innerList = new ArrayList();
innerList.Add(1);
inner.Add("list", innerList);

main.Add("inner", inner);

Hashtable second = (Hashtable)main.Clone();
((ArrayList)((Hashtable)second["inner"])["list"])[0] = 2;

Why does the value within the array change from 1 to 2 in the "main" Hashtable, as the change was made on a clone ?

+4  A: 

You cloned the Hashtable, not its contents.

Daniel Daranas
Thanks for the tip. I have also tried with a new Hashtable(main) and its still the same. Could you please suggest a proper approach ? Thanks.
thelost
If you want to make a deep clone, you'll have to do it manually by iterating over the original collection, cloning each item, and inserting it into a new collection (which you have to create yourself as empty).
Pavel Minaev
A: 

Thanks for your help, guys. I ended up with this solution:

Hashtable clone(Hashtable input)
{
    Hashtable ret = new Hashtable();

    foreach (DictionaryEntry dictionaryEntry in input)
    {
        if (dictionaryEntry.Value is string)
        {
            ret.Add(dictionaryEntry.Key, new string(dictionaryEntry.Value.ToString().ToCharArray()));
        }
        else if (dictionaryEntry.Value is Hashtable)
        {
            ret.Add(dictionaryEntry.Key, clone((Hashtable)dictionaryEntry.Value));
        }
        else if (dictionaryEntry.Value is ArrayList)
        {
            ret.Add(dictionaryEntry.Key, new ArrayList((ArrayList)dictionaryEntry.Value));
        }
    }

    return ret;
}
thelost
This may suit your situation but notice that an ArrayList is still shallow-cloned. If the arraylist contains objects you still have an irregular deep+shallow cloning here.
Henk Holterman
Seems like a decent candidate for an extension method.
Phil