Use unsafe method to clone array : Unsafe Code : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Unsafe Code »

 

Use unsafe method to clone array








    
 

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);
        }
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Unsafe Code