此方法用於從指定的源索引處開始的數組中複製一係列元素,並將其粘貼到從指定的目標索引處開始的另一個Array中。如果複製未完全成功,則確保撤消所有更改。
用法:
public static void ConstrainedCopy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
參數:
-
sourceArray:它是包含要複製的數據的數組。
sourceIndex:它是32位整數,代表複製開始的sourceArray中的索引。
destinationArray:是接收數據的數組。
destinationIndex:這是一個32位整數,代表destinationArray中存儲開始的索引。
length:它是32位整數,代表要複製的元素數。
異常:
- ArgumentNullException:如果sourceArray或destinationArray為null。
- RankException:如果sourceArray和destinationArray具有不同的等級。
- ArrayTypeMismatchException:如果sourceArray類型既不相同,也不派生自destinationArray類型。
- InvalidCastException:無法將sourceArray中的至少一個元素強製轉換為destinationArray的類型。
- ArgumentOutOfRangeException:如果sourceIndex小於sourceArray第一維的下限,或者destinationIndex小於destinationArray第一維的下限,或者length小於零。
- ArgumentException:如果長度大於從sourceIndex到sourceArray末尾的元素數,或者大於從destinationIndex到destinationArray末尾的元素數。
以下示例程序旨在說明Array.ConstrainedCopy(Array,Int32,Array,Int32,Int32)方法的使用:
示例1:
// C# program to demonstrate
// ConstrainedCopy() method
using System;
using System.Collections.Generic;
public class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing new the String
String[] srcArr = { "Sun", "Mon", "Tue", "Thu" };
// Creating the object of empty String Array
String[] destArr = new String[10];
// Display the values of the myArr.
Console.WriteLine("Initial Array:");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(srcArr);
// getting a ConstrainedCopy in destArr
// from srcArr using method ConstrainedCopy()
Array.ConstrainedCopy(srcArr, 1, destArr, 0, 3);
// Display the value of the destArr
Console.WriteLine("Destination Array: ");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(destArr);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (RankException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArrayTypeMismatchException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (InvalidCastException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine("{0}", myArr[i]);
}
Console.WriteLine();
}
}
輸出:
Initial Array: Sun Mon Tue Thu Destination Array: Mon Tue Thu
示例2:對於ArgumentNullException
// C# program to demonstrate
// ConstrainedCopy() method
// For ArgumentNullException
using System;
using System.Collections.Generic;
class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing the
// String Array with null
String[] srcArr = null;
// Creating the object of empty String Array
String[] destArr = new String[10];
// getting a ConstrainedCopy in destArr
// from srcArr using method ConstrainedCopy()
Console.WriteLine("Trying to get the ConstrainedCopy "
+"while srcArr is null");
Console.WriteLine();
Array.ConstrainedCopy(srcArr, 1, destArr, 0, 3);
// Display the value of the destArr.
Console.WriteLine("Destination Array: ");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(destArr);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (RankException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArrayTypeMismatchException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (InvalidCastException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine("{0}", myArr[i]);
}
Console.WriteLine();
}
}
輸出:
Trying to get the ConstrainedCopy while srcArr is null Exception Thrown: System.ArgumentNullException
示例3:對於RankException
// C# program to demonstrate
// ConstrainedCopy() method
// For RankException
using System;
using System.Collections.Generic;
public class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing new the String
String[] srcArr = { "Sun", "Mon", "Tue", "Thu" };
// Creating the object of empty String Array
String[, ] destArr = new String[10, 5];
// Display the values of the myArr.
Console.WriteLine("Initial Array:");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(srcArr);
// getting a ConstrainedCopy in destArr
// from srcArr using method ConstrainedCopy()
Console.WriteLine("Trying to get the ConstrainedCopy "
+"in destArr of rank 2");
Console.WriteLine("while rank of srcArr is 1");
Array.ConstrainedCopy(srcArr, 1, destArr, 0, 3);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (RankException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArrayTypeMismatchException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (InvalidCastException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine("{0}", myArr[i]);
}
Console.WriteLine();
}
}
輸出:
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
示例4:對於ArrayTypeMismatchException
// C# program to demonstrate
// ConstrainedCopy() method
// For ArrayTypeMismatchException
using System;
using System.Collections.Generic;
class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing new the String
String[] srcArr = { "Sun", "Mon", "Tue", "Thu" };
// Creating the object of
// empty Integer Array
int[] destArr = new int[10];
// Display the values of the myArr.
Console.WriteLine("Initial Array:");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(srcArr);
// getting a ConstrainedCopy in destArr
// from srcArr using method ConstrainedCopy()
Console.WriteLine("Trying to get the "
+"ConstrainedCopy in destArr of type int");
Console.WriteLine("but srcArr is of type String");
Array.ConstrainedCopy(srcArr, 1, destArr, 0, 3);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (RankException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArrayTypeMismatchException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (InvalidCastException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine("{0}", myArr[i]);
}
Console.WriteLine();
}
}
輸出:
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
示例5:對於ArgumentOutOfRangeException
// C# program to demonstrate
// ConstrainedCopy() method
// For ArgumentOutOfRangeException
using System;
using System.Collections.Generic;
public class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing
// new the String
String[] srcArr = {"Sun", "Mon", "Tue", "Thu"};
// Creating the object of empty String Array
String[] destArr = new String[10];
// Display the values of the myArr.
Console.WriteLine("Initial Array:");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(srcArr);
// getting a ConstrainedCopy
// in destArr from srcArr
// using method ConstrainedCopy()
Console.WriteLine("Trying to get the ConstrainedCopy"
+" of length less than zero");
Array.ConstrainedCopy (srcArr,1,destArr ,0,-1);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (RankException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArrayTypeMismatchException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (InvalidCastException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine("{0}", myArr[i]);
}
Console.WriteLine();
}
}
輸出:
Initial Array: Sun Mon Tue Thu Trying to get the ConstrainedCopy of length less than zero Exception Thrown: System.ArgumentOutOfRangeException
示例6:對於ArgumentException
// C# program to demonstrate
// ConstrainedCopy() method
// For ArgumentException
using System;
using System.Collections.Generic;
public class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing new the String
String[] srcArr = { "Sun", "Mon", "Tue", "Thu" };
// Creating the object of empty String Array
String[] destArr = new String[10];
// Display the values of the myArr.
Console.WriteLine("Initial Array:");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(srcArr);
// getting a ConstrainedCopy
// in destArr from srcArr
// using method ConstrainedCopy()
Console.WriteLine("Trying to get the ConstrainedCopy"
+" of length is greater than the number ");
Console.WriteLine("of elements from sourceIndex"
+" to the end of sourceArray.");
Console.WriteLine();
Array.ConstrainedCopy(srcArr, 1, destArr, 0, 4);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (RankException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArrayTypeMismatchException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (InvalidCastException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine("{0}", myArr[i]);
}
Console.WriteLine();
}
}
輸出:
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
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Array.ConstrainedCopy() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。