ElementAtOrDefault bugs
See original GitHub issueElementAtOrDefault
is defined as:
/**
* Returns the element at a specified index in a sequence or a default value if the index is out of range.
*/
public ElementAtOrDefault(index: number): T {
return this.ElementAt(index) || undefined
}
However the description doesn’t match the implementation (tho the test does), and the implementation is buggy anyway - c.f. FirstOrDefault()
.
Specifically, indexing off the end (or start, which is not checked for by the way - another bug) should return undefined, rather than throwing, to match the description and .Net behaviour.
Also, if I have a list of numbers that happen to be 0
, and I ask for an index in range, I get undefined
not 0
.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
LINQ: The query operator 'ElementAtOrDefault' is not supported
Forces the query to execute immediately, and; Returns a collection type that supports ElementAtOrDefault(). Hope that helps!
Read more >Bug in example code for Restore soft-deleted blobs when versioning ...
The ElementAtOrDefault(1)?.VersionId should be 0 instead of 1. This also applies to Restore soft-deleted objects when versioning is disabled section ...
Read more >Enumerable.ElementAtOrDefault Method (System.Linq)
Returns the element at a specified index in a sequence or a default value if the index is out of range.
Read more >C# elementatordefault() - is it possible to set your own default
Quote:The default value for reference and nullable types is null. Richard MacCutchan 29-Sep-20 3:26am.
Read more >Questions on Linq and some confusing programming lingo.
On out-of-range accesses, ElementAtOrDefault returns the default value for the type ... with text letter-spacing support, API improvements and bug fixes.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
ElementAt
to also throw for -1 etc;ElementAtOrDefault
to check for out of bounds (either side) explicitly and returnundefined
in those cases;ElementAtOrDefault
to check for in bounds (i.e. theelse
to the aboveif
) and only return the value at that index, with no fallback clause.Sorry for the delay @chris-findlay, it was pretty straight-forward to fix! 😉