Use MemberwiseClone method to clone object : MemberwiseClone : Class C# Examples


C# Examples » Class » MemberwiseClone »

 

Use MemberwiseClone method to clone object









    
using  System;

class  MyValue
{
        public  MyValue(int  count)
        {
                this.count  =  count;
        }
        public  int  count;
}
class  MyObject
{
        public  MyObject(int  count)
        {
                this.contained  =  new  MyValue(count);
        }
        public  MyObject  Clone()
        {
                Console.WriteLine("Clone");
                return((MyObject)  MemberwiseClone());
        }
        public  MyValue  contained;
}
class  MainClass
{
        public  static  void  Main()
        {
                MyObject  my  =  new  MyObject(3);
                MyObject  myClone  =  my.Clone();
                Console.WriteLine("Values:  {0}  {1}",  my.contained.count,  myClone.contained.count);
                myClone.contained.count  =  1;
                Console.WriteLine("Values:  {0}  {1}",  my.contained.count,  myClone.contained.count);
        }
}
    
   
  
   



Output

Clone
Values: 3 3
Values: 1 1


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» MemberwiseClone