Shortes way to copy an complete array and to get the length ?

  • Thread starter Thread starter Pia Stevens
  • Start date Start date
P

Pia Stevens

I have 2 arrays

string[] arr1;
string[] arr2;

Ok there are two way to copy arr1 into arr2:

With Array.Copy()

and

foreach ....

But it seems to me that there is a shorter way when I want to copy the complete array rather
then a subarray.

Could I write simply

arr2 = arr1;

or does this has some side effects ?

Pia
 
How about Clone()? (you need to cast it back)

The line "arr2 = arr1" will simply copy the reference, so you have two
variables / fields pointing to the same array. Fine as long as you
don't start randomly changing things without realising.

..Length won't change between an original or copy - neither will the
performance.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top