コード例 #1
0
    public static void Main()
    {
        MyObject myObject1 = new MyObject(123, new MyRefType(4.56F, 7.89D));
        MyObject myObject2;

        // To obtain a copy that can be modified without affecting the original,
        // this is the wrong way to do it. The reference is copied.
        Console.WriteLine("set myObject2 = myObject1 (copy object references, points to same data)");
        myObject2 = myObject1;
        Console.WriteLine("myObject1: {0}", myObject1.ToString());
        Console.WriteLine("myObject2: {0}", myObject2.ToString());
        Console.WriteLine();
        Console.WriteLine("Set myObject2.Y.i to 9.87");
        myObject2.Y.i = 9.87;
        Console.WriteLine("myObject1: {0}", myObject1.ToString());
        Console.WriteLine("myObject2: {0}", myObject2.ToString());
        Console.WriteLine("Ooops! set myObject1.Y.i to 9.87 as well. Not good if we didn't mean to do that.");

        // Create a new myObject2 that is distinct from myObject1
        myObject2 = new MyObject(12345, new MyRefType(56789.0F, 98765.0D));
        Console.WriteLine();
        Console.WriteLine("This demonstrates how a good copy is made");
        // Perform the cloning operation
        Console.WriteLine("myObject1 = (MyObject) myObject2.Clone() ;");
        myObject1 = (MyObject)myObject2.Clone();
        // Change something in myObject2 which shouldn't be changed in myObject1 if cloning worked.
        Console.WriteLine("Set myObject2.Y.r = 1.23");
        myObject2.Y.r = 1.23F;
        Console.WriteLine("myObject1: {0}", myObject1.ToString());
        Console.WriteLine("myObject2: {0}", myObject2.ToString());
    }
コード例 #2
0
    public static void Main()
    {
        MyObject my      = new MyObject(33);
        MyObject myClone = (MyObject)my.Clone();

        Console.WriteLine("Values: {0} {1}",
                          my.contained.count,
                          myClone.contained.count);
        myClone.contained.count = 15;
        Console.WriteLine("Values: {0} {1}",
                          my.contained.count,
                          myClone.contained.count);
    }
コード例 #3
0
    public static void Main()
    {
        MyObject my      = new MyObject("John", 1001, 3);
        MyObject myClone = my.Clone();

        Console.WriteLine("Values: {0} {1}", my.contained.count, myClone.contained.count);
        Console.WriteLine("Name: {0} {1}", my.Name, myClone.Name);
        myClone.contained.count = 1;
        myClone.Name            = "Robin";
        Console.WriteLine("Values: {0} {1}", my.contained.count, myClone.contained.count);
        Console.WriteLine("Name: {0} {1}", my.Name, myClone.Name);
        //deger atamalı olan degiskenler degişmektedir..
    }
コード例 #4
0
    public static void Main()
    {
        MyObject my = new MyObject(33);

        my.ID = 17;
        MyObject myClone = (MyObject)my.Clone();



        Console.WriteLine("Values: {0} {1}", my.contained.count, myClone.contained.count);
        myClone.contained.count = 15;
        Console.WriteLine("Values: {0} {1}", my.contained.count, myClone.contained.count);


        MyObject em = new MyObject(1131);

        em.ID = 18;
        MyObject cloneEmployee = (MyObject)em.Memberwise_Clone();//Shallow copy Sığ kopyalama

        Console.WriteLine("Values: {0} {1}", em.contained.count, cloneEmployee.contained.count);
        em.contained.count = 56;
        Console.WriteLine("Values: {0} {1}", em.contained.count, cloneEmployee.contained.count);
    }