8
Answers

NP68 Clone()

Maha

Maha

16y
4k
1

Hi Guys

 

NP68 Clone()

 

http://www.java2s.com/Tutorial/CSharp/0220__Data-Structure/Clonearraywithreferencedatainside.htm

 

I got the following program from above website.

 

Either with or without Clone() (highlighted in yellow), program is producing similar output. Anyone knows please explain the reason.

 

Thank you

 

using System;

 

class MyClass

{

    public int Value = 5;

}

 

class MainClass

{

    static void Main()

    {

        MyClass[] orignalArray = new MyClass[3] { new MyClass(), new MyClass(), new MyClass() };

 

        MyClass[] cloneArray = (MyClass[])orignalArray.Clone();

 

        cloneArray[0].Value = 1;

        cloneArray[1].Value = 2;

        cloneArray[2].Value = 3;

 

        foreach (MyClass a in orignalArray)

            Console.WriteLine(a.Value);

 

        foreach (MyClass a in cloneArray)

            Console.WriteLine(a.Value);

    }

}

/*

1

2

3

1

2

3

*/

Answers (8)