Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# array.copy

作者:冷宸枫   发布日期:2025-04-07   浏览:107

using System;

class Program
{
    static void Main()
    {
        // 定义一个源数组
        int[] sourceArray = { 1, 2, 3, 4, 5 };

        // 定义一个目标数组,长度为源数组的两倍
        int[] destinationArray = new int[sourceArray.Length * 2];

        // 使用 Array.Copy 方法将源数组的内容复制到目标数组中
        // 第一个参数是源数组,第二个参数是源数组的起始索引,
        // 第三个参数是目标数组,第四个参数是目标数组的起始索引,
        // 第五个参数是要复制的元素数量
        Array.Copy(sourceArray, 0, destinationArray, 0, sourceArray.Length);

        // 输出目标数组的内容
        Console.WriteLine("Destination array after copying:");
        foreach (int i in destinationArray)
        {
            Console.Write(i + " ");
        }
    }
}

解释说明:

  • sourceArray 是我们要复制的源数组。
  • destinationArray 是我们希望将源数组内容复制到的目标数组。
  • Array.Copy 方法用于将源数组中的元素复制到目标数组中。该方法有多个重载版本,这里使用的是 (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length) 版本。
    • sourceArray: 源数组。
    • sourceIndex: 源数组中的起始索引(从哪里开始复制)。
    • destinationArray: 目标数组。
    • destinationIndex: 目标数组中的起始索引(从哪里开始粘贴)。
    • length: 要复制的元素数量。

通过这段代码,我们可以看到如何使用 Array.Copy 方法来高效地复制数组内容。

上一篇:c# if

下一篇:c# 日期格式

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c#高并发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站