C# Extension Method: Array - ConstrainedCopy

CSharp · Console

/Program.cs

// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002171/array-constrainedcopy

// @nuget: Z.ExtensionMethods

using System;

public class Program
{
	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();
    }
}
;