What is the difference between the System.Array.CopyTo() and System.Array.Clone()

Clear Coding :
         


           

            int[] array1 = { 1, 2, 3, 4 };
            int[] array2 = array1;
            array2[2] = 5;
            Console.WriteLine(array1[2]);
            Console.WriteLine(array2[2]);

            Output :
            5
            5

            int[] array1 = { 1, 2, 8, 4 };
            int[] array2 = (int[])array1.Clone();
            int[] array3 = new int[6];
            array1.CopyTo(array3, 2);

            array2[2] = 5;
            array3[3] = 11;

            Console.WriteLine(array1[3]);
            Console.WriteLine(array2[3]);
            Console.WriteLine(array3[1]);
            Output :
            3
            5





           int[] array1 = { 1, 2, 8, 4 };
            int[] array2 = (int[])array1.Clone();
            int[] array3 = new int[6];// Length should be (number of position(2)+array1Length(4))(6)
            array1.CopyTo(array3, 2);

            array2[2] = 5;
            array3[3] = 11;

            Console.WriteLine(array1[3]);
            Console.WriteLine(array2[3]);
            Console.WriteLine(array3[1]);
 

            int[] array1 = { 1, 2, 3, 4 };
            int[] array2 = array1;
            array2[2] = 5;
            Console.WriteLine(array1[2]);
            Console.WriteLine(array2[2]);

            //Output :
            //5
            //5

            int[] array1 = { 1, 2, 3, 4 };
            int[] array2 = (int[])array1.Clone();
            array2[2] = 5;
            Console.WriteLine(array1[2]);
            Console.WriteLine(array2[2]);

            //Output :
            //3
            //5

            int[] array1 = { 1, 2, 8, 4 };
            int[] array2 = (int[])array1.Clone();
            int[] array3 = new int[6];// Length should be (number of position(2)+array1Length(4))(6)
            array1.CopyTo(array3, 2);

            array2[2] = 5;
            array3[3] = 11;

            Console.WriteLine(array1[3]);
            Console.WriteLine(array2[3]);
            Console.WriteLine(array3[1]);

No comments:

Post a Comment