16 Apr 2014 06:21
Cool post


Included page "inc:signature" does not exist (create it now)
15 Oct 2010 00:24 |
This is the first of a few random posts I plan to make about using C#, covering different aspects of the programming language at an intermediate level (I hope that people learning these languages will benefit from the posts).
Arrays are reference types. Therefore if you do a direct copy you end up with two variables pointing to the exact same array instance (the same place in memory). Modify one, and the other is modified as well.
int[] odd = { 1, 3, 5, 7, 9 }; int[] copy = odd; // odd and copy refer to same array instance Console.WriteLine( odd[0] ); // 1 Console.WriteLine( copy[0] ); // 1 copy[0] = 5; // Change the value at copy[0] Console.WriteLine( odd[0] ); // 5 Console.WriteLine( copy[0] ); // 5
Usually, this is not the behaviour you want!
If you have experience with using similar programming languages (such as C, C++ or Java), then you probably know that the standard way to solve this problem is to use a for loop. You iterate through the array and copy each value one-by-one into the new array.
int[] odd = { 1, 3, 5, 7, 9 }; int[] copy = new int[odd.Length]; // create new array, same size as the original Console.WriteLine( odd[0] ); // 1 Console.WriteLine( copy[0] ); // 0 - default uninitialised value for integers for (int i=0; i<odd.Length; i++) // Iterate through array, one element at a time { copy[i] = odd[i]; } Console.WriteLine( odd[0] ); // 1 Console.WriteLine( copy[0] ); // 1 - copied from first array copy[0] = 5; // Change the value at copy[0] Console.WriteLine( odd[0] ); // 1 Console.WriteLine( copy[0] ); // 5 - modifying copy array doesn't affect odd array
If you are new to C#, you might have noticed a few things that you are unfamiliar with:
However, this is tedious and C# is a language designed to make programming a little easier - so you can spend less time writing code and more time programming. There are shortcuts to some of the more common tasks such as this one.
Because arrays are a reference type in C#, they can have properties (like 'Length') and methods of their own. One of these is the CopyTo method (note: C# is a case-sensitive language, So Case Is Important!).
int[] odd = { 1, 3, 5, 7, 9 }; // Create odd array int[] copy = new int[odd.Length]; // Create copy array of the same size odd.CopyTo(copy, 0); // Copy from 'odd' to 'copy' using the CopyTo method
There are two overloads for the CopyTo method:
array: The one-dimensional array to copy the current one-dimensional array to.
index: Starting index for the copy (0 to copy whole array, higher number to copy only the end of it).
Another way to copy values from one array to another is to use the static method Copy. This is useful if you want to copy the beginning of an array only, because it allows you to specify a length.
int[] odd = { 1, 3, 5, 7, 9 }; int[] copy = new int[odd.Length]; Array.Copy(odd, copy, copy.Length);
Overloads for the Array.Copy method:
You can also use the Clone method to create an array and copy it in just one statement:
int[] odd = { 1, 3, 5, 7, 9 }' int[] copy = (int[]) odd.Clone();
Note that in the example above I'm explicitly casting the result of the Clone method to an integer array. This is because the method actually returns an object.
Whether you are using CopyTo, Array.Copy or Clone, keep in mind that reference types will not be copied and both the original array and the copy will point to the same instance.
If you intend to use your own custom classes or other reference types in an array, the classic for-loop solution is still your best way to go because you have greater control over the process.
Submit Feedback
Please rate this page based on the quality and helpfulness of the content to you.
Average 100% rating from 1 votes.
OLD COMMENT SYSTEM (0 comments)
21 Apr 2014 23:58
Thanks Andre :) Glad it was helpful.
Included page "inc:signature" does not exist (create it now)
16 Jul 2014 20:01
int[] odd = { 1, 3, 5, 7, 9 }'
int[] copy = (int[]) pins.Clone();
Guessing you meant to write:
int[] odd = { 1, 3, 5, 7, 9 };
int[] copy = (int[]) odd.Clone();
?
Included page "inc:signature" does not exist (create it now)
17 Jul 2014 06:58
Thanks Rich, all fixed! =)
Included page "inc:signature" does not exist (create it now)
20 Jan 2016 08:15
thank you, it is helpful!
Included page "inc:signature" does not exist (create it now)
21 May 2016 12:54
My code :
object[,] array = (object[,])Array.Copy((Array)array, new object[13, (int)num + 1], array.Length);
Error :
Error 12 Cannot convert type 'void' to 'object[*,*]'
Please help me…
Thanks so much
Included page "inc:signature" does not exist (create it now)
21 May 2016 13:35
My Code :
long num = 0;
object[,] array;
array = (object[,])Array.Copy((Array)array, new object[13, (int)num + 1 ] , array . Length );
My error :
Error 12 Cannot convert type 'void' to 'object[*,*]'
Please help me…
Thanks so much
Included page "inc:signature" does not exist (create it now)
Post preview:
Close preview