Array.ConstrainedCopy() Method in C#

In this article, we will discuss the Array.ConstrainedCopy() method in C# with its syntax, parameters, and examples.

What is the Array.ConstrainedCopy()?

The Array.ConstrainedCopy() method is used to copy a range of elements from one array to another while ensuring that the operation is carried out properly and within the specified boundaries. This method is very helpful when working with arrays where maintaining memory safety and avoiding buffer overflows are critical.

Syntax:

It has the following syntax:

Parameters:

  • sourceArray: It is the source array from which elements will be copied.
  • sourceIndex: The sourceArray beginning index from where copying begins.
  • destinationArray: The place of the copied elements inside the destination array.
  • destinationIndex: The index in the destinationArray where copying starts at this point.
  • length: The number of elements that must be copied from the source array to the destination array.

Example:

Let us take an example to illustrate the Array.ConstrainedCopy() method in C#.

Output:

Destination Array:
14
25
32
47
85
93

Explanation:

Using the Array.ConstrainedCopy() method, this example demonstrates how to safely copy elements across arrays while handling any possible exceptions that may occur throughout the entire process.

Exceptions:

There are several exceptions in the Array.ConstrainedCopy() method. Some of the exceptions are as follows:

1. ArgumentNullException:

It will occurs, if either the destinationArray or the sourceArray is null.

Example:

Let us take an example to illustrate the Array.ConstrainedCopy() method using ArgumentNullException in C#.

Output:

An error occurred: The value cannot be null.
Parameter name: sourceArray

2. ArgumentOutOfRangeException

If length is less than 0, either the sourceIndex is less than the lower bound of the sourceArray's first dimension, or the destinationIndex is less than the lower bound of the destinationArray's first dimension.

Example:

Letus take an example to illustrate the Array.ConstrainedCopy() method using ArgumentOutOfRangeException in C#.

Output:

Unhandled Exception:
System.ArgumentException: length

Explanation:

As these examples demonstrate, the "Array.ConstrainedCopy()" method throws exceptions when it encounters null arrays or when the length or index parameters are not within a permitted range.

3. RankException

If the sourceArray and destinationArray have different ranks.

Example:

Output:

Error Occurred: Only single-dimension arrays are supported here.

Explanation:

In this example, this code demonstrates how to handle a RankException in C# when transferring elements between arrays of various rankings using the 'Array.ConstrainedCopy()' method.

4. ArgumentException

Suppose the length exceeds the count of elements from destinationIndex to the end of destinationArray. In that case, the length should exceed the count of elements from sourceIndex to the end of sourceArray 'Or'.

Example:

Output:

Error Occurred!: length





Latest Courses