Enumerable.DefaultIfEmpty Method

Definition

Returns the elements of an IEnumerable<T>, or a default valued singleton collection if the sequence is empty.

Overloads

DefaultIfEmpty<TSource>(IEnumerable<TSource>)

Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.

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

Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.

DefaultIfEmpty<TSource>(IEnumerable<TSource>)

Source:
DefaultIfEmpty.cs
Source:
DefaultIfEmpty.cs
Source:
DefaultIfEmpty.cs

Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.

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);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IEnumerable<TSource>

The sequence to return a default value for if it is empty.

Returns

IEnumerable<TSource>

An IEnumerable<T> object that contains the default value for the TSource type if source is empty; otherwise, source.

Exceptions

source is null.

Examples

The following code examples demonstrate how to use DefaultIfEmpty<TSource>(IEnumerable<TSource>) to provide a default value in case the source sequence is empty.

This example uses a non-empty sequence.

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

This example uses an empty sequence.

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

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

/*
 This code produces the following output:

 0
*/

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.

The default value for reference and nullable types is null.

This method can be used to produce a left outer join when it is combined with the GroupJoin method.

See also

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
DefaultIfEmpty.cs
Source:
DefaultIfEmpty.cs
Source:
DefaultIfEmpty.cs

Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.

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

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IEnumerable<TSource>

The sequence to return the specified value for if it is empty.

defaultValue
TSource

The value to return if the sequence is empty.

Returns

IEnumerable<TSource>

An IEnumerable<T> that contains defaultValue if source is empty; otherwise, source.

Examples

The following code example demonstrates how to use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method and specify a default value. The first sequence is not empty and the second sequence is empty.

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

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.

This method can be used to produce a left outer join when it is combined with the GroupJoin method.

See also

Applies to

.NET 9 and other versions
Product Versions
.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