Enumerable.DefaultIfEmpty 方法

定義

傳回 IEnumerable<T>的專案,如果序列是空的,則傳回預設值單一集合。

多載

DefaultIfEmpty<TSource>(IEnumerable<TSource>)

如果序列是空的,則傳回指定序列的專案或單一集合中型別參數的預設值。

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

如果序列是空的,則傳回指定序列的專案或單一集合中的指定值。

DefaultIfEmpty<TSource>(IEnumerable<TSource>)

來源:
DefaultIfEmpty.cs
來源:
DefaultIfEmpty.cs
來源:
DefaultIfEmpty.cs

如果序列是空的,則傳回指定序列的專案或單一集合中型別參數的預設值。

C#
public static System.Collections.Generic.IEnumerable<TSource> DefaultIfEmpty<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
C#
public static System.Collections.Generic.IEnumerable<TSource?> DefaultIfEmpty<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

類型參數

TSource

source專案的型別。

參數

source
IEnumerable<TSource>

如果預設值是空的,則傳回的序列。

傳回

IEnumerable<TSource>

IEnumerable<T> 物件,如果 source 是空的,則包含 TSource 型別的預設值;否則,source

例外狀況

source null

範例

下列程式代碼範例示範如何使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>) 提供預設值,以防來源序列是空的。

此範例使用非空白序列。

C#
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
*/

此範例會使用空序列。

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

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

/*
 This code produces the following output:

 0
*/

備註

這個方法是使用延後執行來實作。 立即傳回值是物件,可儲存執行動作所需的所有資訊。 除非直接呼叫物件 GetEnumerator 方法或在 C# 中使用 foreach,或在 Visual Basic 中使用 For Each,否則不會執行這個方法所代表的查詢。

參考與 Null 型別預設值為 null

當此方法與 GroupJoin 方法結合時,可以用來產生左外部聯結。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

來源:
DefaultIfEmpty.cs
來源:
DefaultIfEmpty.cs
來源:
DefaultIfEmpty.cs

如果序列是空的,則傳回指定序列的專案或單一集合中的指定值。

C#
public static System.Collections.Generic.IEnumerable<TSource> DefaultIfEmpty<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);

類型參數

TSource

source專案的型別。

參數

source
IEnumerable<TSource>

如果指定值是空的,則傳回的序列。

defaultValue
TSource

如果序列是空的,則傳回的值。

傳回

IEnumerable<TSource>

如果 source 是空的,則為包含 defaultValueIEnumerable<T>;否則,source

範例

下列程式代碼範例示範如何使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 方法並指定預設值。 第一個序列不是空的,而第二個序列是空的。

C#
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
*/

備註

這個方法是使用延後執行來實作。 立即傳回值是物件,可儲存執行動作所需的所有資訊。 除非直接呼叫物件 GetEnumerator 方法或在 C# 中使用 foreach,或在 Visual Basic 中使用 For Each,否則不會執行這個方法所代表的查詢。

當此方法與 GroupJoin 方法結合時,可以用來產生左外部聯結。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0