Enumerable.Empty<TResult> メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した型引数を持つ空の IEnumerable<T> を返します。
public:
generic <typename TResult>
static System::Collections::Generic::IEnumerable<TResult> ^ Empty();
public static System.Collections.Generic.IEnumerable<TResult> Empty<TResult>();
static member Empty : unit -> seq<'Result>
Public Function Empty(Of TResult) () As IEnumerable(Of TResult)
型パラメーター
- TResult
返されたジェネリック IEnumerable<T>の型パラメーターに割り当てる型。
返品
型引数がTResult空のIEnumerable<T>。
例
次のコード例では、 Empty<TResult>() を使用して空の IEnumerable<T>を生成する方法を示します。
IEnumerable<decimal> empty = Enumerable.Empty<decimal>();
' Create an empty sequence.
Dim empty As IEnumerable(Of Decimal) = Enumerable.Empty(Of Decimal)()
次のコード例は、 Empty<TResult>() メソッドの使用可能なアプリケーションを示しています。 Aggregate メソッドは、文字列配列のコレクションに適用されます。 コレクション内の各配列の要素は、その配列に 4 つ以上の要素が含まれている場合にのみ、結果の IEnumerable<T> に追加されます。 Empty は、コレクション内に 4 つ以上の要素を持つ配列がない場合、空のシーケンスのみが返されるため、 Aggregate のシード値を生成するために使用されます。
string[] names1 = { "Hartono, Tommy" };
string[] names2 = { "Adams, Terry", "Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
string[] names3 = { "Solanki, Ajay", "Hoeing, Helge",
"Andersen, Henriette Thaulow",
"Potra, Cristina", "Iallo, Lucio" };
List<string[]> namesList =
new List<string[]> { names1, names2, names3 };
// Only include arrays that have four or more elements
IEnumerable<string> allNames =
namesList.Aggregate(Enumerable.Empty<string>(),
(current, next) => next.Length > 3 ? current.Union(next) : current);
foreach (string name in allNames)
{
Console.WriteLine(name);
}
/*
This code produces the following output:
Adams, Terry
Andersen, Henriette Thaulow
Hedlund, Magnus
Ito, Shu
Solanki, Ajay
Hoeing, Helge
Potra, Cristina
Iallo, Lucio
*/
' Create three string arrays.
Dim names1() As String =
{"Hartono, Tommy"}
Dim names2() As String =
{"Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu"}
Dim names3() As String =
{"Solanki, Ajay", "Hoeing, Helge", "Andersen, Henriette Thaulow", "Potra, Cristina", "Iallo, Lucio"}
' Create a List that contains 3 elements, where
' each element is an array of strings.
Dim namesList As New List(Of String())(New String()() {names1, names2, names3})
' Select arrays that have four or more elements and union
' them into one collection, using Empty() to generate the
' empty collection for the seed value.
Dim allNames As IEnumerable(Of String) =
namesList.Aggregate(Enumerable.Empty(Of String)(),
Function(current, nextOne) _
IIf(nextOne.Length > 3, current.Union(nextOne), current))
Dim output As New System.Text.StringBuilder
For Each name As String In allNames
output.AppendLine(name)
Next
' Display the output.
Console.WriteLine(output.ToString())
' This code produces the following output:
'
' Adams, Terry
' Andersen, Henriette Thaulow
' Hedlund, Magnus
' Ito, Shu
' Solanki, Ajay
' Hoeing, Helge
' Potra, Cristina
' Iallo, Lucio
注釈
Empty<TResult>() メソッドは、TResult型の空のシーケンスをキャッシュします。 返されるオブジェクトが列挙されると、要素は生成されません。
場合によっては、このメソッドは、 IEnumerable<T>を受け取るユーザー定義メソッドに空のシーケンスを渡す場合に便利です。 また、 Unionなどのメソッドのニュートラル要素を生成するために使用することもできます。 この Empty<TResult>()の使用例については、「例」セクションを参照してください。