I came across several articles / explanations on shallow copying and deep copy of hash tables, the more I read, the more confused I am.
Hashtable ht = new Hashtable(); ht.Add("1", "hello"); Hashtable ht2 = new Hashtable(); ht2 = ht; // case1: is this shallow copy? ht2["1"] = "H2"; Hashtable ht3 = new Hashtable(ht); // case2: is this shallow copy? ht3["1"] = "H3"; Hashtable ht4 = new Hashtable(); ht4 = (Hashtable)ht.Clone(); // case3: is this shallow copy? ht4["1"] = "H4";
- Case 1: result, changing the contents of ht becomes the same with ht2.
- Case2: result, ht content is different from ht3.
- Case3: result, ht content is different from ht4.
If Case2 and Case3 are shallow copy, should the result not be the same as Case1?
Does this also happen with List, ArrayList, etc.
source share