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>입니다.

예제

다음 코드 예제에서는 빈 IEnumerable<T>생성 하는 Empty<TResult>() 방법을 보여 줍니다.

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>()참조하세요.

적용 대상