List<T>.CopyTo 方法

定義

List<T> 或其中一部分複製到陣列。

多載

CopyTo(T[], Int32)

從目標陣列的指定索引處開始,將整個 List<T> 複製到相容的一維陣列中。

CopyTo(Int32, T[], Int32, Int32)

從目標陣列的指定索引處開始,將項目範圍從 List<T> 複製到相容的一維陣列。

CopyTo(T[])

從目標陣列的開頭開始,將整個 List<T> 複製到相容的一維陣列。

範例

下列範例示範 方法的所有三個 CopyTo 多載。 建立字串的 , List<T> 並填入 5 個字串。 會建立 15 個元素的空字串陣列,並使用 CopyTo(T[]) 方法多載,將清單的所有元素複製到陣列的第一個項目開始的陣列。 方法 CopyTo(T[], Int32) 多載可用來將清單的所有元素複製到陣列索引 6 開頭的陣列, (讓索引 5 空白) 。 最後, CopyTo(Int32, T[], Int32, Int32) 方法多載是用來從清單複製 3 個元素,從索引 2 開始複製到陣列索引 12 開始的陣列, (讓索引 11 空白) 。 接著會顯示陣列的內容。

C#
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Tyrannosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Brachiosaurus");
        dinosaurs.Add("Compsognathus");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        // Declare an array with 15 elements.
        string[] array = new string[15];

        dinosaurs.CopyTo(array);
        dinosaurs.CopyTo(array, 6);
        dinosaurs.CopyTo(2, array, 12, 3);

        Console.WriteLine("\nContents of the array:");
        foreach(string dinosaur in array)
        {
            Console.WriteLine(dinosaur);
        }
    }
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Compsognathus

Contents of the array:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Compsognathus

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Compsognathus

Mamenchisaurus
Brachiosaurus
Compsognathus
 */

CopyTo(T[], Int32)

來源:
List.cs
來源:
List.cs
來源:
List.cs

從目標陣列的指定索引處開始,將整個 List<T> 複製到相容的一維陣列中。

C#
public void CopyTo (T[] array, int arrayIndex);

參數

array
T[]

一維 Array,是從 List<T> 複製過來之項目的目的端。 Array 必須有以零為起始的索引。

arrayIndex
Int32

array 中以零起始的索引,即開始複製的位置。

實作

例外狀況

arraynull

arrayIndex 小於 0。

來源 List<T> 中的項目數目,大於 arrayIndex 到目的 array 結尾的可用空間。

備註

這個方法會使用 Array.Copy 來複製專案。

元素會以逐一查看 List<T>的相同順序複製到 Array

這個方法是 o (n) 作業,其中 nCount

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

CopyTo(Int32, T[], Int32, Int32)

來源:
List.cs
來源:
List.cs
來源:
List.cs

從目標陣列的指定索引處開始,將項目範圍從 List<T> 複製到相容的一維陣列。

C#
public void CopyTo (int index, T[] array, int arrayIndex, int count);

參數

index
Int32

來源 List<T> 中以零為起始的索引,位於複製開始的位置。

array
T[]

一維 Array,是從 List<T> 複製過來之項目的目的端。 Array 必須有以零為起始的索引。

arrayIndex
Int32

array 中以零起始的索引,即開始複製的位置。

count
Int32

要複製的項目數目。

例外狀況

arraynull

index 小於 0。

-或-

arrayIndex 小於 0。

-或-

count 小於 0。

index 等於或大於來源 List<T>Count

-或-

index 到來源 List<T> 結尾的項目數目,大於從 arrayIndex 到目的 array 結尾的可用空間。

備註

這個方法會使用 Array.Copy 來複製專案。

元素會以逐一查看 List<T>的相同順序複製到 Array

這個方法是 o (n) 作業,其中 ncount

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

CopyTo(T[])

來源:
List.cs
來源:
List.cs
來源:
List.cs

從目標陣列的開頭開始,將整個 List<T> 複製到相容的一維陣列。

C#
public void CopyTo (T[] array);

參數

array
T[]

一維 Array,是從 List<T> 複製過來之項目的目的端。 Array 必須有以零為起始的索引。

例外狀況

arraynull

來源 List<T> 中的項目數大於目的 array 可包含的項目數。

備註

這個方法會使用 Array.Copy 來複製專案。

元素會以逐一查看 List<T>的相同順序複製到 Array

這個方法是 o (n) 作業,其中 nCount

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0