First example results in a null reference exception if really used with an empty sequence

Open Beginner friendly
#12,278 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
csharp
Domain
documentation

Research direction

Open xml/System.Linq/Enumerable.xml from the content source URL and locate the DefaultIfEmpty example using Pet and foreach. Replace the example with empty-sequence handling that does not dereference a null Pet, or use the proposed Max example, and update the displayed output so it matches the code.

Written by the indexing model from the issue text.

Description

area-System.Linq untriaged
Type of issue

Typo

Description

The code accesses the property Name of the object pet which, in case of an empty sequence is the default value for the type Pet - a class, therefore null. Personally, I think for loops that do nothing on empty sequences are fine, but for the sake of the example, let's encode an empty sequence handling in the for loop body and examine why this approach is problematic:

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())
    {
        **if (pet == null) Console.WriteLine("No pets");
        else** Console.WriteLine(pet.Name);
    }
}

/*
 This code produces the following output:

 Barley
 Boots
 Whiskers
*/

As can be seen, the condition pet == null is not a good indicator on whether the sequence is actually empty, because it might just contain null instances.
An overall better example would be to use a method that would normally throw when called on an empty sequence, e.g.:

List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6 };
int maximum = numbers.DefaultIfEmpty().Max();
Console.WriteLine(maximum);

numbers = new List<int>();
maximum = numbers.DefaultIfEmpty().Max();
Console.WriteLine(maximum);

/*
This code produces the following output:

6
0
*/

Max from an IEnumerable<int> would throw on an empty sequence, whereas a foreach-loop handles empty sequences just fine.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.defaultifempty?view=net-10.0

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Linq/Enumerable.xml

Document Version Independent Id

8578c8e5-adad-0739-35a4-7d18b7dd3b55

Platform Id

ee397038-ddf6-6808-04ee-0f612db7c91c

Article author

@dotnet-bot

Dominant language
C#
Stars
950
Forks
1.7k
Avg merge
3d 5h
Merged PRs (30d)
38

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/dotnet-api-docs

All issues in dotnet/dotnet-api-docs

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.