Home C# Arrays Methods Clone() C# Array Clone() Method Definition In C#, Clone() creates a new array of the same length and copies each element into it. For value types such as int, the numbers are duplicated. For reference types such as string or custom classes, only the references are copied (a shallow copy).
The method returns object, so cast back to the array type. Array.Copy is another way to copy into an existing target array.
This is an instance member of System.Array. You call it on an array variable with a dot, such as arr.Clone(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax Call Clone on the array instance, then cast the result.
int[] copy = (int[])nums.Clone();
object boxed = names.Clone(); Call What it does (int[])nums.Clone()New int array with the same values as nums. (string[])names.Clone()New string array sharing the same string references.
Shallow copy means nested objects are not deep-cloned. Changing a mutable object through either array affects both.
Library declarations Parameters Parameter Description None Clone() takes no parameters.
Return Value Return Value Description object A new array with the same Length and Rank. Cast to the concrete array type before indexing.
C# Examples 1. Basic clone 2. Shallow refs 3. Same length Example 1 — Clone an Int Array Change the copy and prove the original stays intact.
using System;
class Program {
static void Main() {
int[] nums = { 1, 2, 3 };
int[] copy = (int[])nums.Clone();
copy[0] = 99;
Console.WriteLine("nums: " + string.Join(", ", nums));
Console.WriteLine("copy: " + string.Join(", ", copy));
}
} nums: 1, 2, 3
copy: 99, 2, 3 How It Works New array object. Clone allocates a separate int array.Values copied. At first both hold 1, 2, 3.Independent ints. Writing copy[0] = 99 does not change nums.Cast required. Without (int[]), the compiler only sees object.Example 2 — Shallow Copy with Reference Types Both arrays share the same mutable object reference.
using System;
class Box {
public int Value;
}
class Program {
static void Main() {
Box[] a = { new Box { Value = 5 } };
Box[] b = (Box[])a.Clone();
b[0].Value = 42;
Console.WriteLine("a[0].Value = " + a[0].Value);
Console.WriteLine("b[0].Value = " + b[0].Value);
Console.WriteLine("Same object? " + object.ReferenceEquals(a[0], b[0]));
}
} a[0].Value = 42
b[0].Value = 42
Same object? True How It Works Array shells differ. a and b are different arrays.Element shared. Index 0 in both points at one Box.Mutation shows. Changing Value through b is visible on a.Deep copy needs more. Clone each element yourself if you need independent objects.Example 3 — Clone Keeps Length and Rank Print metadata to confirm the copy matches the source shape.
using System;
class Program {
static void Main() {
string[] names = { "Ann", "Bob" };
string[] copy = (string[])names.Clone();
Console.WriteLine("Length: " + copy.Length);
Console.WriteLine("Rank: " + copy.Rank);
Console.WriteLine("Same array object? " + object.ReferenceEquals(names, copy));
}
} Length: 2
Rank: 1
Same array object? False How It Works Shape preserved. Length and Rank match the source.New identity. ReferenceEquals is false—the arrays are different objects.Strings are immutable. Sharing string references is usually safe.Alternative. Array.Copy can fill an existing destination array instead.Frequently Asked Questions What does Array.Clone do? It returns a new array with the same size and a shallow copy of the elements. The return type is object.
Is Clone a deep copy? No. Reference-type elements still point at the same objects. Only the array shell is new.
How is Clone different from Array.Copy? Clone creates a new array and returns it as object. Copy writes into a destination array you already have.
Do I need to cast Clone? Yes. Cast to int[], string[], or the correct array type before you index it.
Does Clone work on multi-dimensional arrays? Yes. Rank and lengths are preserved in the new array.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful
Helpful Share Copy link Suggestion