Enumerable.DefaultIfEmpty 메서드

정의

IEnumerable<T>의 요소를 반환하거나, 시퀀스가 비어 있으면 기본값이 할당된 singleton 컬렉션을 반환합니다.

오버로드

DefaultIfEmpty<TSource>(IEnumerable<TSource>)

지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 형식 매개 변수의 기본값을 반환합니다.

DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)

지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 singleton 컬렉션의 지정된 값을 반환합니다.

DefaultIfEmpty<TSource>(IEnumerable<TSource>)

지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 형식 매개 변수의 기본값을 반환합니다.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ DefaultIfEmpty(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static System.Collections.Generic.IEnumerable<TSource> DefaultIfEmpty<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
public static System.Collections.Generic.IEnumerable<TSource?> DefaultIfEmpty<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
static member DefaultIfEmpty : seq<'Source> -> seq<'Source>
<Extension()>
Public Function DefaultIfEmpty(Of TSource) (source As IEnumerable(Of TSource)) As IEnumerable(Of TSource)

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IEnumerable<TSource>

비어 있는 경우 기본값을 반환할 시퀀스입니다.

반환

IEnumerable<TSource>

TSource가 비어 있으면 source 형식에 대한 기본값이 들어 있는 IEnumerable<T> 개체이고, 그렇지 않으면 source입니다.

예외

source이(가) null인 경우

예제

다음 코드 예제에서는 소스 시퀀스가 비어 있는 경우 를 사용하여 DefaultIfEmpty<TSource>(IEnumerable<TSource>) 기본값을 제공하는 방법을 보여 줍니다.

이 예제에서는 비어 있지 않은 시퀀스를 사용합니다.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void DefaultIfEmptyEx1()
{
    List<Pet> pets =
        new List<Pet>{ new Pet { Name="Barley", Age=8 },
                       new Pet { Name="Boots", Age=4 },
                       new Pet { Name="Whiskers", Age=1 } };

    foreach (Pet pet in pets.DefaultIfEmpty())
    {
        Console.WriteLine(pet.Name);
    }
}

/*
 This code produces the following output:

 Barley
 Boots
 Whiskers
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub DefaultIfEmptyEx1()
    ' Create a List of Pet objects.
    Dim pets As New List(Of Pet)(New Pet() _
                         {New Pet With {.Name = "Barley", .Age = 8},
                          New Pet With {.Name = "Boots", .Age = 4},
                          New Pet With {.Name = "Whiskers", .Age = 1}})

    Dim output As New System.Text.StringBuilder
    ' Iterate through the items in the List, calling DefaultIfEmpty().
    For Each pet As Pet In pets.DefaultIfEmpty()
        output.AppendLine(pet.Name)
    Next

    ' Display the output.
    Console.WriteLine(output.ToString())
End Sub

' This code produces the following output:
'
' Barley
' Boots
' Whiskers

이 예제에서는 빈 시퀀스를 사용합니다.

List<int> numbers = new List<int>();

foreach (int number in numbers.DefaultIfEmpty())
{
    Console.WriteLine(number);
}

/*
 This code produces the following output:

 0
*/
' Create an empty List.
Dim numbers As New List(Of Integer)()

Dim output As New System.Text.StringBuilder
' Iterate through the items in the List, calling DefaultIfEmpty().
For Each number As Integer In numbers.DefaultIfEmpty()
    output.AppendLine(number)
Next

' Display the output.
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' 0

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다. 이 메서드가 나타내는 쿼리는 개체를 직접 호출 GetEnumerator 하거나 C# 또는 For Each Visual Basic에서 를 사용하여 foreach 개체를 열거할 때까지 실행되지 않습니다.

참조 및 nullable 형식의 기본값은 입니다 null.

이 메서드는 ) 메서드와 결합될 때 왼쪽 외부 조인을 GroupJoin생성하는 데 사용할 수 있습니다.

추가 정보

적용 대상

DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)

지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 singleton 컬렉션의 지정된 값을 반환합니다.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ DefaultIfEmpty(System::Collections::Generic::IEnumerable<TSource> ^ source, TSource defaultValue);
public static System.Collections.Generic.IEnumerable<TSource> DefaultIfEmpty<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);
static member DefaultIfEmpty : seq<'Source> * 'Source -> seq<'Source>
<Extension()>
Public Function DefaultIfEmpty(Of TSource) (source As IEnumerable(Of TSource), defaultValue As TSource) As IEnumerable(Of TSource)

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IEnumerable<TSource>

비어 있는 경우 지정된 값을 반환할 시퀀스입니다.

defaultValue
TSource

시퀀스가 비어 있는 경우에 반환할 값입니다.

반환

IEnumerable<TSource>

defaultValue가 비어 있으면 source가 들어 있는 IEnumerable<T>이고, 그렇지 않으면 source입니다.

예제

다음 코드 예제에서는 메서드를 사용 하 고 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 기본값을 지정 하는 방법을 보여 줍니다. 첫 번째 시퀀스가 비어 있지 않고 두 번째 시퀀스가 비어 있습니다.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void DefaultIfEmptyEx2()
{
    Pet defaultPet = new Pet { Name = "Default Pet", Age = 0 };

    List<Pet> pets1 =
        new List<Pet>{ new Pet { Name="Barley", Age=8 },
                       new Pet { Name="Boots", Age=4 },
                       new Pet { Name="Whiskers", Age=1 } };

    foreach (Pet pet in pets1.DefaultIfEmpty(defaultPet))
    {
        Console.WriteLine("Name: {0}", pet.Name);
    }

    List<Pet> pets2 = new List<Pet>();

    foreach (Pet pet in pets2.DefaultIfEmpty(defaultPet))
    {
        Console.WriteLine("\nName: {0}", pet.Name);
    }
}

/*
 This code produces the following output:

 Name: Barley
 Name: Boots
 Name: Whiskers

 Name: Default Pet
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub DefaultIfEmptyEx2()
    ' Create a Pet object to use as the default value.
    Dim defaultPet As New Pet With {.Name = "Default Pet", .Age = 0}

    ' Create a List of Pet objects.
    Dim pets1 As New List(Of Pet)(New Pet() _
                          {New Pet With {.Name = "Barley", .Age = 8},
                           New Pet With {.Name = "Boots", .Age = 4},
                           New Pet With {.Name = "Whiskers", .Age = 1}})

    Dim output1 As New System.Text.StringBuilder
    ' Enumerate the items in the list, calling DefaultIfEmpty()
    ' with a default value.
    For Each pet As Pet In pets1.DefaultIfEmpty(defaultPet)
        output1.AppendLine("Name: " & pet.Name)
    Next

    ' Display the output.
    Console.WriteLine(output1.ToString())

    ' Create an empty List.
    Dim pets2 As New List(Of Pet)

    Dim output2 As New System.Text.StringBuilder
    ' Enumerate the items in the list, calling DefaultIfEmpty()
    ' with a default value.
    For Each pet As Pet In pets2.DefaultIfEmpty(defaultPet)
        output2.AppendLine("Name: " & pet.Name)
    Next

    ' Display the output.
    Console.WriteLine(output2.ToString())
End Sub

' This code produces the following output:
'
' Name: Barley
' Name: Boots
' Name: Whiskers
'
' Name: Default Pet

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다. 이 메서드가 나타내는 쿼리는 개체를 직접 호출 GetEnumerator 하거나 C# 또는 For Each Visual Basic에서 를 사용하여 foreach 개체를 열거할 때까지 실행되지 않습니다.

이 메서드는 ) 메서드와 결합될 때 왼쪽 외부 조인을 GroupJoin생성하는 데 사용할 수 있습니다.

추가 정보

적용 대상