Difference between System.Array.CopyTo() and System.Array.Clone() ? [Resolved]

Posted by Akiii under C# on 6/9/2011 | Points: 10 | Views : 11954 | Status : [Member] | Replies : 4
What's the difference between the System.Array.CopyTo() and System.Array.Clone()?

Thanks and Regards
Akiii




Responses

Posted by: Ndebata on: 6/9/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi
Clone() will return an object (Which holds the shallow copy of the array).
Here the array will be same as that of the source one which is cloned.


But in case of copy to you have the flexibily to start copy from a postion.

int[] abc = new int[2];
abc[0] = 1;
abc[1] = 2;
int[] ctarr=new int[5];
abc.CopyTo(ctarr, 2);

so ctarr contents will be 0|0|1|2|0;

Thanks,
Debata

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/9/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
ctarr is an array of integer whose length is 5.
now you are going to copy array abc ( an array having two elements ).

As the start index has been set as 2 in CopyTo function, it will start copy from index 2 onwards till all the elements of source array (abc in our case) has been copied.

As integer ctarr is an integer array , default value is zero.
so before copy its values were 0|0|0|0|0
Now after copyto it will be
0|0|1|2|0

Thanks,
Debata

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/9/2011 [Member] Bronze | Points: 25

Up
0
Down
hi narayan...

Can u just explain two lines to me from the above code that you have provided ?

abc.CopyTo(ctarr, 2);


so ctarr contents will be 0|0|1|2|0;


Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/9/2011 [Member] Bronze | Points: 25

Up
0
Down
excellent explanation narayan.....its crystal clear to me now..

Thanks for ur help..
Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response