MGZ NetArt

Software Development and Support

Home
Design Patterns
C# Tips
AsyncCallback
Asynchronous Form.ShowDia
BackgroundWorker
Connection String
CopyTo() vs Clone()
Convert Color To String
Dispose vs Finalize
Debug mode
Delete Duplicate in SQL
Drag&Drop for TreeView
Edit Full Path Property
Hiding And Overriding
Polimorphism
How to use Resources
Run Time DLL
Simple Threading
Singleton Connection
Save Win Form as JPG File
Save Object in Registry
Soap Serialization
Sort and CompareTo
Working With List<>
Examples & Videos
MRDS Examples
Resume
Contact Us
What is the difference between the System.Array.CopyTo() and System.Array.Clone()?
If you ever use them, you can see the difference.

Here is an example:

object[] myarray = new object[] { "one", 2, "three", 4, "really big number", 2324573984927361 };

           

//create shallow copy by CopyTo

//You have to instantiate your new array first

object[] myarray2 = new object[myarray.Length];

//but then you can specify how many members of original array you would like to copy

myarray.CopyTo(myarray2, 0);

           

//create shallow copy by Clone

object[] myarray1;

//here you don't need to instantiate array,

//but all elements of the original array will be copied

myarray1 = myarray.Clone() as object[];


//if not sure that we create a shalow copy lets test it

myarray[0] = 1;

Console.WriteLine(myarray[0]);// prints 1

Console.WriteLine(myarray1[0]);//prints "one"

Console.WriteLine(myarray2[0]);//prints "one"