Enumerable.Empty<TResult> 方法

定義

傳回具有指定之型別引數的空白 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> 的型別參數。

傳回

IEnumerable<TResult>

其型別引數為 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 會套用至字串陣列的集合。 只有在該陣列包含四個以上的元素時,集合中每個陣列的元素才會加入至產生的 IEnumerable<T>Empty 用來產生 的種子值 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>() 的範例,請參閱範例一節。

適用於