Create copy of an array in C#
This post will discuss how to create a copy of an array in C#.
1. Using Array.CopyTo method
The idea is to create a new array of the same length as the source array, and call the Array.CopyTo() method to copy all elements from the source array to the destination array starting at the specified destination array index.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { int[] arr = { 5, 4, 7, 2, 9 }; int[] copy = new int[arr.Length]; arr.CopyTo(copy, 0); Console.WriteLine(String.Join(", ", copy)); // 5, 4, 7, 2, 9 } } |
The Array.Copy() method can also work here. The following code example shows how it can be used to copy the specified number of elements from the source array to the destination array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { int[] arr = { 5, 4, 7, 2, 9 }; int[] copy = new int[arr.Length]; Array.Copy(arr, copy, arr.Length); Console.WriteLine(String.Join(", ", copy)); // 5, 4, 7, 2, 9 } } |
The Array.Copy() method is overloaded to copy a range of array elements starting at the specified source index to another array starting at the specified destination index.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; public class Example { public static void Main() { int[] arr = { 5, 4, 7, 2, 9 }; int start = 1; int length = 3; int[] copy = new int[length]; Array.Copy(arr, start, copy, 0, length); Console.WriteLine(String.Join(", ", copy)); // 4, 7, 2 } } |
2. Using LINQ
LINQ’s Take() method is often used to get the first few elements from a sequence. To create a copy of an array, you can pass the number of elements in the source array to the Take() method and get the array representation of the returned sequence using ToArray() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Linq; public class Example { public static void Main() { int[] arr = { 5, 4, 7, 2, 9 }; int[] copy = arr.Take(arr.Length).ToArray(); Console.WriteLine(String.Join(", ", copy)); // 4, 7, 2 } } |
3. Using Array.Clone method
Another plausible option is to use the Array.Clone method to create a shallow copy of the array. The following code example demonstrates its usage. Note that the Array.Clone method returns an Object and a cast is needed to convert it to an array.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int[] arr = { 5, 4, 7, 2, 9 }; int[] copy = (int[])arr.Clone(); Console.WriteLine(String.Join(", ", copy)); // 4, 7, 2 } } |
4. Using Buffer.BlockCopy method
The Buffer.BlockCopy method copies the specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset. This is the best option in terms of speed since it directly accesses the underlying memory.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; public class Example { public static void Main() { int[] arr = { 5, 4, 7, 2, 9 }; int[] copy = new int[arr.Length]; int length = arr.Length * sizeof(int); Buffer.BlockCopy(arr, 0, copy, 0, length); Console.WriteLine(String.Join(", ", copy)); // 4, 7, 2 } } |
5. Using For Loop
Finally, you can write a custom routine for copying an array. The idea is to allocate space for the destination array and copy each element from the source array to the destination array using a standard for-loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; public class Example { public static void Main() { int[] arr = { 5, 4, 7, 2, 9 }; int[] copy = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) { copy[i] = arr[i]; } Console.WriteLine(String.Join(", ", copy)); // 4, 7, 2 } } |
That’s all about creating a copy of an array in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)