Enumerable.Empty Enumerable.Empty Enumerable.Empty Enumerable.Empty Method

定義

指定した型引数を持つ空の IEnumerable<T> を返します。Returns an empty IEnumerable<T> that has the specified type argument.

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> の型パラメーターに割り当てる型。The type to assign to the type parameter of the returned generic IEnumerable<T>.

戻り値

IEnumerable<TResult>

型引数が TResult である空の IEnumerable<T>An empty IEnumerable<T> whose type argument is TResult.

次のコード例は、使用する方法を示しますEmpty<TResult>()空を生成するIEnumerable<T>します。The following code example demonstrates how to use Empty<TResult>() to generate an empty IEnumerable<T>.

IEnumerable<decimal> empty = Enumerable.Empty<decimal>();
' Create an empty sequence.
Dim empty As IEnumerable(Of Decimal) = Enumerable.Empty(Of Decimal)()

次のコード例に示しますの可能性のあるアプリケーション、Empty<TResult>()メソッド。The following code example demonstrates a possible application of the Empty<TResult>() method. Aggregateメソッドは、文字列の配列のコレクションに適用されます。The Aggregate method is applied to a collection of string arrays. 結果に、コレクション内の各配列の要素が追加IEnumerable<T>その配列には、次の 4 つ以上の要素が含まれている場合のみです。The elements of each array in the collection are added to the resulting IEnumerable<T> only if that array contains four or more elements. Empty シード値を生成するために使用Aggregateのため、配列、コレクション内に 4 つ以上の要素が存在しない場合、空のシーケンスだけが返されます。Empty is used to generate the seed value for Aggregate because if no array in the collection has four or more elements, only the empty sequence is returned.

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.
MsgBox(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します。The Empty<TResult>() method caches an empty sequence of type TResult. 返されるオブジェクトが列挙されたときに要素は生成されません。When the object it returns is enumerated, it yields no elements.

場合によっては、このメソッドはユーザー定義のメソッドに空のシーケンスを渡すことのIEnumerable<T>します。In some cases, this method is useful for passing an empty sequence to a user-defined method that takes an IEnumerable<T>. などのメソッドのニュートラル要素を生成するも使用できますUnionします。It can also be used to generate a neutral element for methods such as Union. この使用例については例を参照してくださいEmpty<TResult>()します。See the Example section for an example of this use of Empty<TResult>().

適用対象