question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ElementAtOrDefault bugs

See original GitHub issue

ElementAtOrDefault 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chris-findlaycommented, Jan 13, 2019
  • Fixing ElementAt to also throw for -1 etc;
  • Fixing ElementAtOrDefault to check for out of bounds (either side) explicitly and return undefined in those cases;
  • Fixing ElementAtOrDefault to check for in bounds (i.e. the else to the above if) and only return the value at that index, with no fallback clause.
0reactions
kutyelcommented, Jun 12, 2019

Sorry for the delay @chris-findlay, it was pretty straight-forward to fix! 😉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found