This method is used to copy a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely.
public static void ConstrainedCopy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
sourceArray: It is the Array that contains the data to copy.
sourceIndex: Its is the 32-bit integer that represents the index in the sourceArray at which copying begins.
destinationArray: It is the Array that receives the data.
destinationIndex: It is the 32-bit integer that represents the index in the destinationArray at which storing begins.
length: It is the 32-bit integer that represents the number of elements to copy.
- ArgumentNullException: If the sourceArray or destinationArray is null.
- RankException: If the sourceArray and destinationArray have different ranks.
- ArrayTypeMismatchException: If the sourceArray type is neither the same as nor derived from the destinationArray type.
- InvalidCastException: At least one element in sourceArray cannot be cast to the type of destinationArray.
- ArgumentOutOfRangeException: If the sourceIndex is less than the lower bound of the first dimension of sourceArray or destinationIndex is less than the lower bound of the first dimension of destinationArray or length is less than zero.
- ArgumentException: If the length is greater than the number of elements from sourceIndex to the end of sourceArray Or the length is greater than the number of elements from destinationIndex to the end of destinationArray.
Below programs illustrate the use of Array.ConstrainedCopy(Array, Int32, Array, Int32, Int32) Method:
Initial Array: Sun Mon Tue Thu Destination Array: Mon Tue Thu
Example 2: For ArgumentNullException
Trying to get the ConstrainedCopy while srcArr is null Exception Thrown: System.ArgumentNullException
Example 3: For RankException
Initial Array: Sun Mon Tue Thu Trying to get the ConstrainedCopy in destArr of rank 2 while rank of srcArr is 1 Exception Thrown: System.RankException
Example 4: For ArrayTypeMismatchException
Initial Array: Sun Mon Tue Thu Trying to get the ConstrainedCopy in destArr of type int but srcArr is of type String Exception Thrown: System.ArrayTypeMismatchException
Example 5: For ArgumentOutOfRangeException
Initial Array: Sun Mon Tue Thu Trying to get the ConstrainedCopy of length less than zero Exception Thrown: System.ArgumentOutOfRangeException
Example 6: For ArgumentException
Initial Array: Sun Mon Tue Thu Trying to get the ConstrainedCopy of length is greater than the number of elements from sourceIndex to the end of sourceArray. Exception Thrown: System.ArgumentException
- Difference between Method Overriding and Method Hiding in C#
- C# | Uri.IsBaseOf(Uri) Method
- C# | CopyTo() Method
- Stack.Pop() Method in C#
- C# | Math.Log() Method
- C# | Uri.FromHex() Method
- C# | Uri.IsHexDigit() Method
- C# | Math.Tan() Method
- C# | String.Contains() Method
- C# | Math.Sin() Method
- C# | IndexOfAny() Method
- C# | Math.Cos() Method
- C# | Trim() Method
- C# | Dictionary.Add() Method
- Decimal.Add() Method in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.