英語で読む

次の方法で共有


Enumerable.ToList<TSource>(IEnumerable<TSource>) メソッド

定義

IEnumerable<T> から List<T> を作成します。

C#
public static System.Collections.Generic.List<TSource> ToList<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

IEnumerable<T> の作成元の List<T>

戻り値

List<TSource>

入力シーケンスの要素を含む List<T>

例外

sourcenullです。

次のコード例では、 を使用 ToList してクエリの即時評価を強制し、クエリ結果を含む を List<T> 返す方法を示します。

C#
string[] fruits = { "apple", "passionfruit", "banana", "mango",
                      "orange", "blueberry", "grape", "strawberry" };

List<int> lengths = fruits.Select(fruit => fruit.Length).ToList();

foreach (int length in lengths)
{
    Console.WriteLine(length);
}

/*
 This code produces the following output:

 5
 12
 6
 5
 6
 9
 5
 10
*/

注釈

メソッドは ToList<TSource>(IEnumerable<TSource>) 、クエリの即時評価を強制し、クエリ結果を含む を返 List<T> します。 クエリ結果のキャッシュされたコピーを取得するために、このメソッドをクエリに追加できます。

ToArray は同様の動作をしますが、 の代わりに配列を List<T>返します。

適用対象

製品 バージョン
.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 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