업데이트: 2007년 11월
지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 singleton 컬렉션의 지정된 값을 반환합니다.
네임스페이스:
System.Linq 어셈블리:
System.Core(System.Core.dll)
<ExtensionAttribute> _
Public Shared Function DefaultIfEmpty(Of TSource) ( _
source As IEnumerable(Of TSource), _
defaultValue As TSource _
) As IEnumerable(Of TSource)
Dim source As IEnumerable(Of TSource)
Dim defaultValue As TSource
Dim returnValue As IEnumerable(Of TSource)
returnValue = source.DefaultIfEmpty(defaultValue)
public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
this IEnumerable<TSource> source,
TSource defaultValue
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IEnumerable<TSource>^ DefaultIfEmpty(
IEnumerable<TSource>^ source,
TSource defaultValue
)
J#에서는 제네릭 API를 사용할 수 있지만 새로 선언할 수는 없습니다.
JScript에서는 제네릭 형식이나 메서드를 지원하지 않습니다.
Type 매개 변수
- TSource
source 요소의 형식입니다.
사용 정보
Visual Basic 및 C#에서는 이 메서드를 IEnumerable<(Of <(TSource>)>) 형식의 모든 개체에서 인스턴스 메서드로 호출할 수 있습니다. 인스턴스 메서드 구문을 사용하여 이 메서드를 호출할 경우에는 첫 번째 매개 변수를 생략합니다. 자세한 내용은 확장 메서드(Visual Basic) 또는 확장 메서드(C# 프로그래밍 가이드)를 참조하십시오.
이 메서드는 지연된 실행을 사용하여 구현됩니다. 해당 작업을 수행하는 데 필요한 모든 정보가 저장된 개체가 즉시 반환됩니다. 이 메서드에서 나타내는 쿼리는 해당 GetEnumerator 메서드를 직접 호출하거나, foreach(Visual C#의 경우) 또는 For Each(Visual Basic의 경우)를 사용하여 개체를 열거할 때까지 실행되지 않습니다.
이 메서드와 GroupJoin) 메서드를 함께 사용하여 왼쪽 우선 외부 조인을 만들 수 있습니다.
다음 코드 예제에서는 DefaultIfEmpty<(Of <(TSource>)>)(IEnumerable<(Of <(TSource>)>), TSource) 메서드를 사용하여 기본값을 지정하는 방법을 보여 줍니다. 첫 번째 시퀀스는 비어 있지 않고 두 번째 시퀀스는 비어 있습니다.
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.
MsgBox(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.
MsgBox(output2.ToString())
End Sub
' This code produces the following output:
'
' Name: Barley
' Name: Boots
' Name: Whiskers
'
' Name: Default Pet
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
*/
Windows Vista, Windows XP SP2, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
.NET Framework 및 .NET Compact Framework에서 모든 플랫폼의 전체 버전을 지원하지는 않습니다. 지원되는 버전의 목록을 보려면 .NET Framework 시스템 요구 사항을 참조하십시오.
.NET Framework
3.5에서 지원
.NET Compact Framework
3.5에서 지원
참조
기타 리소스