Use unsafe method to clone array

image_pdfimage_print

public struct MyValue
{
public int id;
public MyValue(int id) { this.id = id; }
}
class ClassAddressApp
{
unsafe public static MyValue[] CloneMyValues(MyValue[] box)
{
MyValue[] ret = new MyValue[box.Length];
fixed (MyValue* src = box, dest = ret)
{
MyValue* pSrc = src;
MyValue* pDest = dest;
for (int index = 0; index < box.Length; index++) { *pDest = *pSrc; pSrc++; pDest++; } } return ret; } static void Main(string[] args) { MyValue[] box = new MyValue[2]; box[0] = new MyValue(1); box[1] = new MyValue(2); MyValue[] bag = CloneMyValues(box); foreach (MyValue i in bag) { Console.WriteLine(i.id); } } } [/csharp]