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

定義

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::List<TSource> ^ ToList(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static System.Collections.Generic.List<TSource> ToList<TSource>(this System.Collections.Generic.IEnumerable<TSource> source);
static member ToList : seq<'Source> -> System.Collections.Generic.List<'Source>
<Extension()>
Public Function ToList(Of TSource) (source As IEnumerable(Of TSource)) As List(Of TSource)

型パラメーター

TSource

sourceの要素の型。

パラメーター

source
IEnumerable<TSource>

IEnumerable<T>を作成するList<T>

返品

List<TSource>

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

例外

sourcenullです。

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

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
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "passionfruit", "banana", "mango",
 "orange", "blueberry", "grape", "strawberry"}

' Project the length of each string and
' put the length values into a List object.
Dim lengths As List(Of Integer) =
fruits _
.Select(Function(fruit) fruit.Length) _
.ToList()

' Display the results.
Dim output As New System.Text.StringBuilder
For Each length As Integer In lengths
    output.AppendLine(length)
Next
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' 5
' 12
' 6
' 5
' 6
' 9
' 5
' 10

注釈

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

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

適用対象