複製陣列

使用靜態 Array.Copy() 方法複製部分陣列,從源和目標中的索引 0 開始:

var sourceArray = new int[] { 11, 12, 3, 5, 2, 9, 28, 17 };
var destinationArray= new int[3];
Array.Copy(sourceArray, destinationArray, 3);

// destinationArray will have 11,12 and 3

使用 CopyTo() 例項方法複製整個陣列,從源的索引 0 和目標中的指定索引開始:

var sourceArray = new int[] { 11, 12, 7 };
var destinationArray = new int[6];
sourceArray.CopyTo(destinationArray, 2);

// destinationArray will have 0, 0, 11, 12, 7 and 0

Clone 用於建立陣列物件的副本。

var sourceArray = new int[] { 11, 12, 7 };
var destinationArray = (int)sourceArray.Clone();

//destinationArray will be created and will have 11,12,17.

CopyToClone 都執行淺拷貝,這意味著內容包含與原始陣列中的元素相同的物件的引用。