Possible duplicate:
C #: any faster way to copy arrays?
I have an array of structures like this:
struct S { public long A; public long B; } ... S[] s1 = new S[1000000]; ... S[] = new S[s1.Length];
I can use unsafe mode and copy the source array of structures to an array of bytes, and then from the byte array to the target array of structures. But that means that I have to allocate a huge intermediate array of bytes. Is there any way to avoid this? Is it possible to somehow represent the destination array as a byte array and copy it right there?
unsafe { int size = Marshal.SizeOf(s0[0]) * s0.Length; byte[] tmp = new byte[size]; fixed (var tmpSrc = &s0[0]) { IntPtr src = (IntPtr)tmpSrc; Marchal.Copy(tmpSrc, 0, tmp, 0, size); }
source share