-
C# |数组。约束复制()方法
C# |数组。约束复制()方法
原文:https://www . geeksforgeeks . org/c-sharp-array-constrained copy-method/
此方法用于从指定的源索引开始复制数组中的一系列元素,并将它们粘贴到从指定的目标索引开始的另一个数组中。保证如果复制没有完全成功,所有更改都将被撤消。
语法:
public static void ConstrainedCopy (Array sourceArray, int sourceIndex,
Array destinationArray, int destinationIndex, int length);
参数:
源数组:是包含要复制的数据的数组。 源索引:它是代表源数组中开始复制的索引的 32 位整数。 目的阵列:接收数据的是阵列。 目标索引:是代表目标数组中开始存储的索引的 32 位整数。 长度:表示要复制的元素数量的 32 位整数。
异常:
- ArgumentNullException: 如果 sourceArray 或 destinationArray 为空。
- rankeexception:如果 sourceArray 和 destinationArray 的等级不同。
- ArrayTypeMismatchException:如果 sourceArray 类型与 destinationArray 类型既不相同也不从 destination array 类型派生。
- InvalidCastException:source array 中至少有一个元素不能转换为 destinationArray 的类型。
- ArgumentOutOfRangeException:如果 sourceIndex 小于 sourceArray 第一维度的下界或 destinationIndex 小于 destinationArray 第一维度的下界或长度小于零。
- 参数异常:如果长度大于从源索引到源数组末尾的元素数或长度大于从目标索引到目标数组末尾的元素数。
下面的程序说明了数组的使用。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();
}
}
Output:
Initial Array:
Sun
Mon
Tue
Thu
Destination Array:
Mon
Tue
Thu
示例 2: 适用于参数异常
// 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();
}
}
Output:
Trying to get the ConstrainedCopy while srcArr is null
Exception Thrown: System.ArgumentNullException
例 3: 为rankeexception
// 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();
}
}
Output:
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: 为数组类型不匹配异常
// 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();
}
}
Output:
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: 为argumentout of range exception
// 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();
}
}
Output:
Initial Array:
Sun
Mon
Tue
Thu
Trying to get the ConstrainedCopy of length less than zero
Exception Thrown: System.ArgumentOutOfRangeException
示例 6: 适用于参数异常
// 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();
}
}
Output:
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
参考:
- https://docs . Microsoft . com/en-us/dotnet/API/system . array . constrained copy?视图=netframework-4.7.2
版权属于:月萌API www.moonapi.com,转载请注明出处
本文链接:https://www.moonapi.com/news/27803.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数