I have class with a List
member. If I want to clone an instance of this class, do I need a deep copy or the MemberwiseClone()
shallow co
This entirely depends on how you plan to use the copy.
If you do a shallow copy like
List x = new List() { 1, 2, 3, 4, 5 };
List y = x;
y[2] = 4;
Then x will contain {1, 2, 4, 4, 5 }
If you do a deep copy of the list:
List x = new List { 1, 2, 3, 4, 5 };
List y = new List(x);
y[2] = 4;
Then x will contain { 1, 2, 3, 4, 5 } and y will contain { 1, 2, 4, 4, 5 }
How you plan on using the copy really determines whether you use shallow or deep copies.