Skip to content

SingleOrDefault with a default value reads it as a predicate #760

Description

@edgarmesquita

What happens

SingleOrDefault(source, defaultValue) and SingleOrDefault(source, predicate, defaultValue), .NET 6's overloads, are lowered as the overloads without a default value (SingleStrategy), the sibling of #720:

  • new int[0].SingleOrDefault(9) is 9 in .NET and a TypeError in the browser ([].find(9));
  • new[] { 1, 2, 3 }.SingleOrDefault(x => x > 5, 9) is 9 in .NET and 0 in the browser.

#715's null-argument measurement listed SingleOrDefault(IEnumerable<TSource>, TSource) as a gap of its parameter's type, so it was not counted among #720's overloads.

Expected

The one element, or the one that matches, a null element included; the default value handed where there is none; and the InvalidOperationException .NET throws for a second element ("Sequence contains more than one element") or a second match ("Sequence contains more than one matching element"), the predicate having run on every element up to it.

Found by the review of the pull request that fixes #720.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions