C# 中的 Array.Copy() 方法用于将一个数组的元素复制到另一个数组中,可以在不同的位置进行复制。它的语法如下:

Array.Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);

参数说明:

  • sourceArray:源数组,即要复制的数组。
  • sourceIndex:源数组中要开始复制的索引位置。
  • destinationArray:目标数组,即要复制到的数组。
  • destinationIndex:目标数组中要开始复制的索引位置。
  • length:要复制的元素数量。

示例代码如下:

int[] sourceArray = { 1, 2, 3, 4, 5 };
int[] destinationArray = new int[5];

Array.Copy(sourceArray, 0, destinationArray, 0, 5);

foreach (int num in destinationArray)
{
    Console.WriteLine(num);
}

输出结果为:

1
2
3
4
5

上述代码中,将 sourceArray 数组中的元素复制到 destinationArray 数组中,并从索引位置 0 开始复制。


原文地址: https://www.cveoy.top/t/topic/o2Od 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录