Clone array with reference data inside : Array Clone « Data Structure « C# / CSharp Tutorial






using System;

class MyClass
{
   public int Value = 5;
}

class MainClass
{
   static void Main()
   {
      MyClass[] orignalArray = new MyClass[3] { new MyClass(), new MyClass(), new MyClass() }; 
      
      MyClass[] cloneArray = (MyClass[])orignalArray.Clone();                   

      cloneArray[0].Value = 1;                               
      cloneArray[1].Value = 2;
      cloneArray[2].Value = 3;

      foreach (MyClass a in orignalArray)
         Console.WriteLine(a.Value);

      foreach (MyClass a in cloneArray)
         Console.WriteLine(a.Value);
   }
}
1
2
3
1
2
3








11.11.Array Clone
11.11.1.Clone array with reference data inside
11.11.2.Clone Value Array
11.11.3.Cloning arrays