Re: Hashtable.Clone() is shadow copy, how to depth copy hashtable?
Hello Daniel O'Connell [C# MVP],
thanks!
copy construct can get new object, such as :
public class Test
{
private int a;
private string s;
private Hashtable h;
public Test()
{
a = 10;
s = "hello";
h = new Hashtable;
}
public Test(Test t)
{
a = t.a;
s = t.s;
h = t.h;//copy?
h = (Hashtable)t.h.Clone() // shadow copy
}
public void func()
{
ClassA testa = new ClassA();
ClassA testb = new ClassA();
h.Add("a",testa);
h.Add("b",testb);
a = 15;
}
}

void main()
{
private Test a = new Test();
a.func();
private Test b = new Test(a); //here b.h have testa and testb
and b.a = 15
//object b is a new object, I want b.h is a new Hashtable
private Test c = new Test(); // here c.h has no element and
c.a = 10
c = a; // c is a refrence ,not I want
}