IT Community - Software Programming, Web Development and Technical Support

CopyTo and Clone

This is a discussion on CopyTo and Clone within the C# Programming forums, part of the Software Development category; In C#; between the System.Array.CopyTo() and System.Array.Clone(), what are their differences? Where can you use copyTo ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 04-05-2007, 08:32 AM
nhoj nhoj is offline
D-Web Programmer
 
Join Date: Apr 2007
Posts: 96
nhoj is on a distinguished road
Default CopyTo and Clone

In C#; between the System.Array.CopyTo() and System.Array.Clone(), what are their differences? Where can you use copyTo and Clone in different ways?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-06-2007, 12:52 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Thumbs up Re: CopyTo and Clone

The first one performs a deep copy of the array, the second one is shallow.

The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identacle object.

Array.CopyTo()
Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array.

Array.Clone Method
Creates a shallow copy of the Array.

A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.


thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Karpagarajan : 04-06-2007 at 12:56 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-17-2007, 03:40 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default Re: CopyTo and Clone

System.Array.CopyTo() method copies all the elements of the current one dimensional Array to the specified one dimensional Array.

System.Array.Clone method creates a shallow copy of the Array.

A shallow copy of Array copies only the element of the Array, whether they are referenced types or value types, but it does not copy the object that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to. In contrast the deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-17-2007, 05:27 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Default Re: CopyTo and Clone

Hi,
I couldnt able to understand the difference between copyTo() and Clone().
Is there any sample coding which contains these two methods used in it
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-17-2007, 06:09 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default Re: CopyTo and Clone Sample Code C#

Hi Krishna kumar,

CopyTo(destinationArray, Index)

It copies the current instance of the array to the destination array. The copy will start from the Index specified in the argument. So, if Index is set to zero, it means, it copies all the elements of the source array to destination array.

string[] arr=new string[2];
string[] arrTemp=new string[5];

arr[0] = "Sandip";
arr[1] = "Calcutta";
arr.CopyTo(arrTemp,2);

This C# code snippet clones an entire array using Array.Clone.

using System;

class CloneArray
{
public static void Main()
{
string[] array1 = {"a", "b", "c"};
string[] array2 = (string[]) array1.Clone();
DumpArray ("Original array: ", array1);
DumpArray ("Cloned array: ", array2);
}

private static void DumpArray (string title, string[] array)
{
Console.Write (title);
foreach (string s in array)
{
Console.Write ("{0} ", s);
}
Console.WriteLine();
}
}

I hope this may help....

thnx....
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can anybody explain about the differences between dataset.clone and dataset.copy? kingmaker C# Programming 2 08-28-2007 12:22 AM
What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ? Archer C# Programming 2 08-25-2007 03:00 AM


All times are GMT -7. The time now is 08:48 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0