Showing posts with label DEEP CLONE. Show all posts
Showing posts with label DEEP CLONE. Show all posts

Thursday, July 1, 2010

DEEP CLONE USING ICLONEABLE INTERFACE

.NET objects are of two types: value and reference. Variables of value type objects hold the object bits themselves and have "copy-on-assignment" behavior. Variables of reference (ref) type are actually pointers to memory. That is, when you create a new variable of ref type and assign it to an existing object, you are actually creating another pointer to the same memory.

A deep copy of an object
duplicates everything directly or indirectly referenced by the fields in the
object.It provides a mechanism to duplicate an object using an object's persistence mechanism (IPersistStream).

Example Programe
using System;
public class one{public int a;}
public class two:ICloneable
{
public int b;
public one o=new one();
public two(int a1,int b1)
{
o.a=a1;
b=b1;
}
public void show(){Console.WriteLine(o.a+" "+b);}
public Object Clone()
{
two t=new two(o.a,b);
return t;
}
}
class twoclone
{
public static void Main(string[] args)
{
two t1=new two(1,2);
two t2=(two)t1.Clone();
t1.show();
t2.show();
t1.o.a=5;
t2.b=5;
t1.show();
t2.show();
}
}