Enumerable.Empty<TResult> Yöntem

Tanım

Belirtilen tür bağımsız değişkenine sahip boş IEnumerable<T> bir değer döndürür.

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)

Tür Parametreleri

TResult

Döndürülen genel IEnumerable<T>türünün type parametresine atanacak tür.

Döndürülenler

IEnumerable<TResult>

Tür bağımsız değişkeni olan boş IEnumerable<T> bir TResult.

Örnekler

Aşağıdaki kod örneği, boş IEnumerable<T>bir oluşturmak için nasıl kullanılacağını Empty<TResult>() gösterir.

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

Aşağıdaki kod örneği, yönteminin Empty<TResult>() olası bir uygulamasını gösterir. Aggregate yöntemi, dize dizileri koleksiyonuna uygulanır. Koleksiyondaki her dizinin öğeleri, yalnızca bu dizi dört veya daha fazla öğe içeriyorsa sonuca IEnumerable<T> eklenir. Empty için tohum değerini oluşturmak için Aggregate kullanılır çünkü koleksiyondaki dizide dört veya daha fazla öğe yoksa yalnızca boş dizi döndürülür.

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

Açıklamalar

Empty<TResult>() yöntemi, türünde TResultboş bir diziyi önbelleğe alır. Döndürdüğü nesne numaralandırıldığında hiçbir öğe vermez.

Bazı durumlarda, bu yöntem bir alan IEnumerable<T>kullanıcı tanımlı bir yönteme boş bir dizi geçirmek için yararlıdır. Gibi yöntemler Unioniçin nötr bir öğe oluşturmak için de kullanılabilir. Bu kullanımın Empty<TResult>()bir örneği için Örnek bölümüne bakın.

Şunlara uygulanır