Home C# Arrays Methods ConstrainedCopy() C# Array ConstrainedCopy() Method Definition In C#, Array.ConstrainedCopy copies a range from a source array to a destination array, similar to Array.Copy. The special rule: if copying fails partway (for example a bad cast), the destination is restored so you do not keep a half-updated buffer.
Use it when a partial copy would leave invalid data. For simple same-type copies that cannot fail mid-way, Array.Copy is enough.
This is a static member of System.Array. You call it with the class name, such as Array.ConstrainedCopy(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax Pass source, source index, destination, destination index, and length—just like the long Copy overload.
Array.ConstrainedCopy(src, 0, dest, 0, src.Length); Call What it does Array.ConstrainedCopy(src, 0, dest, 0, 3)Copies 3 elements with rollback protection on failure.
Same-type value copies almost never fail mid-way. ConstrainedCopy matters more when element conversion can throw.
Library declarations public static void ConstrainedCopy(Array sourceArray, int sourceIndex,
Array destinationArray, int destinationIndex, int length); Parameters Parameter Description sourceArray Required. Array to read. sourceIndex Starting index in the source. destinationArray Required. Array to write. destinationIndex Starting index in the destination. length Number of elements to copy.
Return Value Return Value Description void On success, destination holds the copied range. On failure, destination is unchanged (rolled back).
C# Examples 1. Basic constrained 2. Same type 3. Vs Copy note Example 1 — Successful ConstrainedCopy Copy ints the safe way when you want the constrained API.
using System;
class Program {
static void Main() {
int[] src = { 1, 2, 3 };
int[] dest = { 9, 9, 9, 9 };
Array.ConstrainedCopy(src, 0, dest, 1, 3);
Console.WriteLine(string.Join(", ", dest));
}
} How It Works Offset dest. Writing starts at destination index 1.Three values. 1, 2, 3 replace the last three nines.First kept. dest[0] stays 9.Success path. No exception, so nothing is rolled back.Example 2 — Full Same-Type Copy Replace an entire destination of equal length.
using System;
class Program {
static void Main() {
string[] src = { "red", "green" };
string[] dest = { "x", "y" };
Array.ConstrainedCopy(src, 0, dest, 0, 2);
Console.WriteLine(string.Join(" | ", dest));
}
} How It Works Exact fit. Length 2 into length 2 fills every slot.Overwrite. Old x and y are replaced.Strings copy by reference. The array slots get the same string references.API choice. ConstrainedCopy documents “no partial update” intent.Example 3 — When to Prefer ConstrainedCopy Print a reminder and still perform a normal successful copy.
using System;
class Program {
static void Main() {
object[] src = { 10, 20 };
object[] dest = { 0, 0 };
Array.ConstrainedCopy(src, 0, dest, 0, 2);
Console.WriteLine(dest[0] + "," + dest[1]);
Console.WriteLine("Use ConstrainedCopy when conversion might fail mid-copy.");
}
} 10,20
Use ConstrainedCopy when conversion might fail mid-copy. How It Works Object arrays. Element assignment can involve casts in other scenarios.Here it succeeds. Both values land cleanly.Mental model. Think “transactional copy” for the destination range.Simple ints. Day-to-day int[] copies usually use Array.Copy.Frequently Asked Questions What does ConstrainedCopy do? It copies a range like Array.Copy, but rolls back the destination if the operation fails before finishing.
When should I use ConstrainedCopy instead of Copy? When a mid-copy failure must not leave partially updated destination data.
Does ConstrainedCopy always allocate a backup? The runtime ensures the destination appears unchanged on failure. Treat it as the safer option for fallible element copies.
Is ConstrainedCopy slower? It can be slightly more work than Copy. For same-type copies that cannot fail, Copy is enough.
Do indexes work the same as Copy? Yes. You pass sourceIndex, destinationIndex, and length the same way.
Next: ConvertAll() Create a new array by converting each element with a converter function with System.Array.
ConvertAll() tutorial → 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.
11 people found this page helpful
Helpful Share Copy link Suggestion