473,383 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Array.Clone versus Array.CopyTo

Hi Guys,

Would anyone mind telling me the difference between Array.Clone and
Array.CopyTo? I was asked this during an interview and got partial
credit for saying they were basically the same because they create
shallow copies. I ran ILDasm on Array and saw that they do the copy
using different IL calls. Does anyone know why one would be preferred
over the other?

Thanks,
James

May 21 '07 #1
2 11589
Clone will return a new copy, CopyTo will copy the elements into an
array that you allocate.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"james" <ja********@gmail.comwrote in message
news:11**********************@x18g2000prd.googlegr oups.com...
Hi Guys,

Would anyone mind telling me the difference between Array.Clone and
Array.CopyTo? I was asked this during an interview and got partial
credit for saying they were basically the same because they create
shallow copies. I ran ILDasm on Array and saw that they do the copy
using different IL calls. Does anyone know why one would be preferred
over the other?

Thanks,
James
May 21 '07 #2
On May 21, 5:03 am, james <james.w...@gmail.comwrote:
Hi Guys,

Would anyone mind telling me the difference between Array.Clone and
Array.CopyTo? I was asked this during an interview and got partial
credit for saying they were basically the same because they create
shallow copies. I ran ILDasm on Array and saw that they do the copy
using different IL calls. Does anyone know why one would be preferred
over the other?

Thanks,
James
Hi,

Array.Clone creates a shallow copy of the array, whilst Array.CopyTo
copies all the elements of the array to the specified array
(preallocated).

With CopyTo you can copy the elements to the destination array
starting at a specified destination array index, thus add elements to
a given array.

Besides, as Nich mentioned, Clone does the allocation it self and
returns an exact length array.

Cheers,
Moty

May 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: y | last post by:
Hi, I have an array with 2 elements and I would like to keep the elements but redimension the array so that I can add another value. How do I do this. I have tried this but, i think I have...
6
by: Amil Hanish | last post by:
I have two classes that I have implemented ICloneable. From my top-level class, how to I clone the base class values: See "how do I clone the base class values" below. Since Clone returns an...
1
by: Craig | last post by:
Hey Everyone - I'm trying to determine the fastest way of moving array information around and could use some help. Here's the setup: Class A stores a DateTime and an amount (a payment)....
4
by: Brian Keating | last post by:
Hi there, Consider this from MSDN *Notes to Inheritors When you derive from DataGridViewCheckBoxCell and add new properties to the derived class, be sure to override the Clone method to copy the...
0
by: jg | last post by:
I have trouble marshalling array I have a VB COM class TestArrClass Imports System.Runtime.Remoting Imports System.Runtime.InteropServices <ComClass(TestArrClass.ClassId,...
0
by: gg | last post by:
I have trouble marshalling array I have a VB COM class TestArrClass Imports System.Runtime.Remoting Imports System.Runtime.InteropServices <ComClass(TestArrClass.ClassId,...
18
by: active | last post by:
Console.WriteLine(String.Equals(s, s.Clone.ToString)) s is type string. This returns True. If Clone really made a new copy I'd expect False.
3
by: Dr1ZZ | last post by:
I've made this short test code, to illustrate my problem: int j = new int; j = new int { 0 }; int k = (int)j.Clone(); k = 1;
1
by: Tony Johansson | last post by:
Hello! Below is an example of an easy way to shuffle a Deck I must admit it's not the most efficient way of doing it but the array is small so the time it's take is hardy noticed at all. But...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.