Clone array with reference data inside : Array Clone : Data Structure C# Examples


C# Examples » Data Structure » Array Clone »

 

Clone array with reference data inside









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



Output

1
2
3
1
2
3


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» Array Clone