Skip to content

DefaultIfEmptyIterator<TSource>.TryGetElementAt returns _default when not found #119834

@skyoxZ

Description

@skyoxZ

Description

public override TSource? TryGetElementAt(int index, out bool found)
{
TSource? item = _source.TryGetElementAt(index, out found);
if (found)
{
return item;
}
if (index == 0)
{
found = true;
}
return _default;
}

Iterator<TSource>.TryGetElementAt should return default(TSource) when not found but here it returns _default.

Reproduction Steps

int result = Array.Empty<int>().DefaultIfEmpty(1).ElementAtOrDefault(1);
Console.WriteLine(result);

Expected behavior

Outputs 0.

Actual behavior

Outputs 1.

Regression?

The bug was introduced in .NET 9.

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions