What is the difference between Clone and CopyTo methods in .NET?

Clone and CopyTo methods belong to System.Array namespace. Purpose of each method along with their differences is tabulated below:



Clone
CopyTo
Clone method returns a new array containing all elements copied from the array on which clone method is called. CopyTo method copies elements of the array into an existing array. Elements will be copied into a specific index and its increments.
The array size of the destination array need not be mentioned. Array size of the destination array has to be declared before triggering copyTo method.
If the array size declared is smaller to fit in elements, the array size will automatically grow based on the number of elements that are getting copied. Array size should be large enough to fit both existing array elements and the elements to be copied. If the size is smaller than the actual number of elements, then you will get the following error while performing CopyTo:
"Destination array was not long enough. Check destIndex and length, and the array's lower bounds."
Here is an example of Clone method:
class sampleClass{
public static void Main() {
int[] array1 = new int[] {10,20,30};
int[] array2 = array1.Clone() as int[];
for(int i=0;i<array2.Length;i++) {
Console.Write(array2[i]+" ");
}
}
}
Output of this code will be:
10 20 30
Here is an example of CopyTo method:
class sampleClass{
public static void Main() {
int[] array1 = new int[] {10,20,30};
int[] array2 =
new int[array1.Length];
array1.CopyTo(array2,0);
for(int i=0;i<array2.Length;i++) {
Console.Write(array2[i]+" ");
}
}
}
Output of this code will be:
10 20 30

 

| Can you call a constructor from another constructor of the Class in .NET? | Difference between Response.Output.Write() method and Response.Write() method in .NET | How do you establish multiple inheritance in C#? | How do you introduce a ReadOnly property in C#? | How do you perform constructor overloading in C#? | Is catch(Exception) recommended to be used in .NET? | What are the different access modifiers available in C#? | What are the different ways of overloading in C#? | What are the members of stringbuilder class in C#? | What is Multicast Delegate? Explain it with example in C# | What is the difference between abstract class and interface in .NET? | What is the difference between Clone and CopyTo methods in .NET | What is the difference between const and readonly in .NET | What is the difference between directcast and ctype in .NET? | What is the difference between out and ref parameters in .NET | What is the difference between public assembly and private assembly in .NET | What is the difference between strong typing and weak typing in .NET? | What is the difference between Trace and Debug in .NET | What is the need for Abstract Factory Pattern in C#? | What is the need for Adapter Pattern in C# |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.