How to force a deep copy when copying structs with arrays?

Posted by Danvil on Stack Overflow See other posts from Stack Overflow or by Danvil
Published on 2010-04-24T21:15:58Z Indexed on 2010/04/24 21:23 UTC
Read the original article Hit count: 195

Filed under:

If have a

struct A {
  public double[] Data;
  public int X;
}

How can I force a deep copy when using operator= or adding instances of A to a container?

The problem is for example:

A a = new A();
var list = new List<A>();
list.Add(a); // does not make a deep copy of Data
A b = a; // does not make a deep copy of Data

Do I really have to implement my own DeepClone method and call it every time? This would be extremly error-prone ...

© Stack Overflow or respective owner

Related posts about c#