First example results in a null reference exception if really used with an empty sequence
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/dotnet-api-docs
-
untriaged
Difficulty 1/5 Under an hour Newbie friendliness 88/100
dotnet/dotnet-api-docs#13039 · 1 comment ·
-
DisplayAttribute string properties: Remarks incorrectly state the property performs resource lookup Openarea-System.ComponentModel.DataAnnotations untriaged
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
dotnet/dotnet-api-docs#13023 · 1 comment ·
-
area-System.Net.Sockets untriaged
Difficulty 1/5 Under an hour Newbie friendliness 88/100
dotnet/dotnet-api-docs#13022 · 1 comment ·
-
area-System.Runtime untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
dotnet/dotnet-api-docs#13015 · 1 comment ·
-
area-System.Runtime.InteropServices untriaged
Difficulty 1/5 Under an hour Newbie friendliness 84/100
dotnet/dotnet-api-docs#12959 ·
All issues in dotnet/dotnet-api-docs
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·