Advertisement
Guest User

Cloning by copy-constructors

a guest
Oct 31st, 2013
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1.     //http://stackoverflow.com/questions/78536/deep-cloning-objects-in-c-sharp/78551#comment11411812_78577
  2.     class CloningTest
  3.     {
  4.         public static void PerformTests()
  5.         {
  6.             Type1 t = new Type1();
  7.             t.x = 1; t.y = 11; t.c = new Type2();
  8.             t.c.x = 2; t.c.z = 222; t.c.q = new Type3();
  9.             t.c.q.x = 3; t.c.q.w = 3333;
  10.             Type1 k = new Type1(t);
  11.             k.x = 9; k.c.z = 666;
  12.             System.Console.WriteLine(t.x + " ; " + t.c.z);
  13.         }
  14.  
  15.         abstract class AbstractBaseType
  16.         {
  17.             public int x;
  18.             public abstract void DummyMethod();
  19.             public AbstractBaseType() { }
  20.             public AbstractBaseType(AbstractBaseType src) { x = src.x; }
  21.         }
  22.         class Type1 : AbstractBaseType
  23.         {
  24.             public int y; public Type2 c;
  25.             public override void DummyMethod() { }
  26.             public Type1() { x = 0; }
  27.             public Type1(Type1 src) : base(src) { y = src.y; c = new Type2(src.c); }
  28.         }
  29.         class Type2 : AbstractBaseType
  30.         {
  31.             public int z; public Type3 q;
  32.             public override void DummyMethod() { }
  33.             public Type2() { }
  34.             public Type2(Type2 src) : base(src) { z = src.z; q = new Type3(src.q); }
  35.         }
  36.         class Type3 : AbstractBaseType
  37.         {
  38.             public int w;
  39.             public override void DummyMethod() { }
  40.             public Type3() { }
  41.             public Type3(Type3 src) : base(src) { w = src.w; }
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement