C# Array ConstrainedCopy() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

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.

C#
Array.ConstrainedCopy(src, 0, dest, 0, src.Length);
CallWhat 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
C#
public static void ConstrainedCopy(Array sourceArray, int sourceIndex,
    Array destinationArray, int destinationIndex, int length);

Parameters

ParameterDescription
sourceArrayRequired. Array to read.
sourceIndexStarting index in the source.
destinationArrayRequired. Array to write.
destinationIndexStarting index in the destination.
lengthNumber of elements to copy.

Return Value

Return ValueDescription
voidOn success, destination holds the copied range. On failure, destination is unchanged (rolled back).

C# Examples

Example 1 — Successful ConstrainedCopy

Copy ints the safe way when you want the constrained API.

C#
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

  1. Offset dest. Writing starts at destination index 1.
  2. Three values. 1, 2, 3 replace the last three nines.
  3. First kept. dest[0] stays 9.
  4. Success path. No exception, so nothing is rolled back.

Example 2 — Full Same-Type Copy

Replace an entire destination of equal length.

C#
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

  1. Exact fit. Length 2 into length 2 fills every slot.
  2. Overwrite. Old x and y are replaced.
  3. Strings copy by reference. The array slots get the same string references.
  4. API choice. ConstrainedCopy documents “no partial update” intent.

Example 3 — When to Prefer ConstrainedCopy

Print a reminder and still perform a normal successful copy.

C#
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.");
    }
}

How It Works

  1. Object arrays. Element assignment can involve casts in other scenarios.
  2. Here it succeeds. Both values land cleanly.
  3. Mental model. Think “transactional copy” for the destination range.
  4. Simple ints. Day-to-day int[] copies usually use Array.Copy.

Frequently Asked Questions

It copies a range like Array.Copy, but rolls back the destination if the operation fails before finishing.
When a mid-copy failure must not leave partially updated destination data.
The runtime ensures the destination appears unchanged on failure. Treat it as the safer option for fallible element copies.
It can be slightly more work than Copy. For same-type copies that cannot fail, Copy is enough.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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