C# Array - ConstrainedCopy() Method



The C# Array ConstrainedCopy() method is used to copy range of element from one array to another. At the specified source index and pastes them to another Array starting at the specified destination index.

This method guarantees that all changes are undone if the copy does not succeed completely.

Exception

There are the following exceptions ofConstrainedCopy()methods −

  • ArgumentNullException: Thrown if either sourceArray and destinationArray is null.
  • RankException: Thrown if the array have different number of dimensions.
  • ArrayTypeMismatchException: Thrown if the type of sourceArray does not compatible with destinationArray.
  • InvalidCastException: Thrown if elements in the sourceArray cannot be cast to the type of destinationArray.
  • ArgumentOutOfRangeException: Thrown if sourceIndex, destinationIndex, or length is out of range.
  • ArgumentException Thrown if the range to copy is invalid.

Syntax

Following is the syntax of the C# Array ConstrainedCopy() method −

public static void ConstrainedCopy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);

Parameters

This method accepts the following parameters −

  • sourceArray: The array that contains data to copy.
  • sourceIndex: A 32-bit integer that represents the index in the sourceArray at which copying begins.
  • destinationArray: The array that received the data.
  • destinationIndex: A 32-bit integer that represents the index in the destinationArray at which storing begins.
  • length: The number of element to copy.

Return value

This method does not return any value it modifies the destination array.

Example 1: Basic Example

Let us crate a basic example, how we use the ConstrainedCopy() method in the C# code −

using System;
class Program
{
   public static void Main()
   {
      int[] source = { 1, 2, 3, 4, 5 };
      int[] destination = { 10, 20, 30, 40, 50 };

      Array.ConstrainedCopy(source, 1, destination, 2, 2);
      Console.WriteLine(string.Join(", ", destination));
   }
}

Output

Following is the output −

10, 20, 2, 3, 50

Example 2: Handling Reference Types

Let us see another example of theConstrainedCopy()method. The first two elements of source are copied into destination starting at index 1 −

using System;
class Program
{
   public static void Main()
   {
      string[] source = { "apple", "banana", "cherry" };
      string[] destination = { "orange", "grape", "kiwi" };

      Array.ConstrainedCopy(source, 0, destination, 1, 2);

      Console.WriteLine(string.Join(", ", destination));
   }
}

Output

Following is the output −

orange, apple, banana

Example 3: Exception Safety

In this example we uses ConstrainedCopy() to perform exception safety, even though an exception occurs, the destination array remains unchanged due to the atomic behaviour of ConstrainedCopy −

using System;
class Program
{
   public static void Main()
   {
      int[] source = { 1, 2, 3, 4, 5 };
      int[] destination = { 10, 20, 30, 40, 50 };

      try {
         Array.ConstrainedCopy(source, 3, destination, 4, 3); 
      } catch (Exception ex) {
         Console.WriteLine($"Error: {ex.Message}");
      }
      Console.WriteLine(string.Join(", ", destination));
   }
}

Output

Following is the output −

Error: length
10, 20, 30, 40, 50

Example 4: Exception ArrayTypeMismatchException

In this example we uses ConstrainedCopy() method to check both source and destination type is same or not. If not then it thrown ArrayTypeMismatchException −

using System;
class Program
{
   public static void Main()
   {
      object[] source = { "Hello", 123, "World" };
      string[] destination = new string[3];

      try {
         Array.ConstrainedCopy(source, 0, destination, 0, 3);
      } catch (ArrayTypeMismatchException ex) {
         Console.WriteLine($"Error: {ex.Message}");
      }
   }
}

Output

Following is the output −

ERROR!
Unhandled Exception:
System.InvalidCastException
csharp_array_class.htm
Advertisements