Array - ConstrainedCopy

Copies a range of elements from an starting at the specified source index and pastes them to another starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely.

Try it

public static void Main()
{
	int[] sourceArray = { 2, 5, 3, 8, 6, 5, 4, 15, 19, 32 };
    Array destinationArray = Array.CreateInstance(typeof(Int32), 10);
	destinationArray.SetValue(8, 0);
    destinationArray.SetValue(2, 1);

	// C# Extension Method: Array - ConstrainedCopy
    sourceArray.ConstrainedCopy(0, destinationArray,2, 5);

    // Displays the values of the Array.
    Console.WriteLine( "The Int32 array contains the following:" );
    PrintValues(destinationArray);
}

public static void PrintValues(Array myArr)
{
    int i = 0;
    int cols = myArr.GetLength(myArr.Rank - 1);
    foreach (object o in myArr)
    {
        if ( i < cols )
        {
            i++;
        }
        else
        {
            Console.WriteLine();
            i = 1;
        }
        Console.Write( "\t{0}", o);
    }
    Console.WriteLine();
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     Copies a range of elements from an  starting at the specified source index and pastes them to another
    ///     starting at the specified destination index.  Guarantees that all changes are undone if the copy does not
    ///     succeed completely.
    /// </summary>
    /// <param name="sourceArray">The  that contains the data to copy.</param>
    /// <param name="sourceIndex">A 32-bit integer that represents the index in the  at which copying begins.</param>
    /// <param name="destinationArray">The  that receives the data.</param>
    /// <param name="destinationIndex">A 32-bit integer that represents the index in the  at which storing begins.</param>
    /// <param name="length">A 32-bit integer that represents the number of elements to copy.</param>
    public static void ConstrainedCopy(this Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
    {
        Array.ConstrainedCopy(sourceArray, sourceIndex, destinationArray, destinationIndex, length);
    }
}