0
0
CsharpHow-ToBeginner · 3 min read

How to Copy Array in C#: Syntax and Examples

In C#, you can copy an array using Array.Copy, Clone(), or by manually copying elements with a loop. Array.Copy copies elements from one array to another, while Clone() creates a shallow copy of the entire array.
📐

Syntax

Here are common ways to copy arrays in C#:

  • Array.Copy(sourceArray, destinationArray, length): Copies elements from sourceArray to destinationArray.
  • var newArray = (Type[])oldArray.Clone(): Creates a shallow copy of the entire array.
  • Manual copy using a for loop to copy elements one by one.
csharp
Array.Copy(sourceArray, destinationArray, length);

var newArray = (Type[])oldArray.Clone();

for (int i = 0; i < oldArray.Length; i++)
{
    newArray[i] = oldArray[i];
}
💻

Example

This example shows how to copy an integer array using Array.Copy and Clone(). It prints both copied arrays to confirm the copy.

csharp
using System;

class Program
{
    static void Main()
    {
        int[] original = { 1, 2, 3, 4, 5 };

        // Using Array.Copy
        int[] copy1 = new int[original.Length];
        Array.Copy(original, copy1, original.Length);

        // Using Clone
        int[] copy2 = (int[])original.Clone();

        Console.WriteLine("Copy using Array.Copy:");
        foreach (var item in copy1)
        {
            Console.Write(item + " ");
        }
        Console.WriteLine();

        Console.WriteLine("Copy using Clone():");
        foreach (var item in copy2)
        {
            Console.Write(item + " ");
        }
    }
}
Output
Copy using Array.Copy: 1 2 3 4 5 Copy using Clone(): 1 2 3 4 5
⚠️

Common Pitfalls

One common mistake is to assign one array to another like int[] copy = original;. This does not copy the array but only copies the reference, so changes to one affect the other.

Also, Clone() creates a shallow copy, so if the array contains reference types, the objects themselves are not copied.

csharp
int[] original = { 1, 2, 3 };
int[] copy = original; // This copies the reference, not the array
copy[0] = 100;
Console.WriteLine(original[0]); // Outputs 100, original changed too

// Correct way:
int[] copyCorrect = (int[])original.Clone();
copyCorrect[0] = 100;
Console.WriteLine(original[0]); // Outputs 1, original unchanged
Output
100 1
📊

Quick Reference

Summary of array copy methods in C#:

MethodDescriptionNotes
Array.Copy(source, dest, length)Copies specified elements from source to destination arrayDestination array must be initialized with enough size
Clone()Creates a shallow copy of the entire arrayGood for simple arrays of value types
Manual for-loopCopy elements one by oneUseful for custom copy logic or partial copy
Assignment (=)Copies reference only, not the arrayAvoid if you want a true copy

Key Takeaways

Use Array.Copy to copy elements between arrays with control over length and positions.
Clone() creates a shallow copy of the entire array quickly for value types.
Avoid assigning arrays directly if you want a separate copy; it only copies the reference.
Manual copying with a loop gives full control but is more verbose.
Be careful with reference types inside arrays; Clone() does not deep copy objects.