Copy an array
using System;
class MainClass {
public static void Main() {
int[] source = { 21, 22, 23, 24, 25 };
int[] target = { 11, 12, 13, 14, 15 };
int[] source2 = { -1, -2, -3, -4, -5 };
Array.Copy(source, target, source.Length);
Console.Write("target after copy: ");
foreach(int i in target)
Console.Write(i + " ");
Console.WriteLine();
}
}
target after copy: 21 22 23 24 25
Copy an array: Copy into middle of target
using System;
class MainClass {
public static void Main() {
int[] source = { 1, 2, 3, 4, 5 };
int[] target = { 11, 12, 13, 14, 15 };
int[] source2 = { -1, -2, -3, -4, -5 };
Array.Copy(source2, 2, target, 3, 2);
Console.Write("target after copy: ");
foreach(int i in target)
Console.Write(i + " ");
Console.WriteLine();
}
}
target after copy: 11 12 13 -3 -4
Copying elements between arrays
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
public class MainClass
{
public static void Main()
{
int[] srcArray = new int[] { 1, 2, 3 };
int[] destArray = new int[] { 4, 5, 6, 7, 8, 9 };
Array.Copy(srcArray, destArray, srcArray.Length);
srcArray.CopyTo(destArray, 0);
srcArray.CopyTo(destArray, 3);
Array.Copy(srcArray, 0, destArray, 2, srcArray.Length);
}
}