Skip to content

Commit 7365770

Browse files
github-actions[bot]Copiloteiriktsarpalis
authored
[release/10.0] Fix DefaultIfEmptyIterator.TryGetElementAt returning default value for out-of-bounds indices (#119845)
* Initial plan * Fix DefaultIfEmptyIterator.TryGetElementAt to return default when element not found Co-authored-by: eiriktsarpalis <[email protected]> * Add regression test and finalize fix for DefaultIfEmptyIterator.TryGetElementAt Co-authored-by: eiriktsarpalis <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: eiriktsarpalis <[email protected]>
1 parent 0abfd87 commit 7365770

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.SpeedOpt.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections;
56
using System.Collections.Generic;
67

@@ -77,9 +78,10 @@ public override int GetCount(bool onlyIfCheap)
7778
if (index == 0)
7879
{
7980
found = true;
81+
return _default;
8082
}
8183

82-
return _default;
84+
return default;
8385
}
8486

8587
public override bool Contains(TSource value)

src/libraries/System.Linq/tests/DefaultIfEmptyTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,36 @@ public void First_Last_ElementAt()
124124
Assert.Equal(42, empty.DefaultIfEmpty(42).ElementAt(0));
125125
Assert.Throws<ArgumentOutOfRangeException>(() => empty.DefaultIfEmpty(42).ElementAt(1));
126126
}
127+
128+
[Fact]
129+
public void ElementAtOrDefault_OutOfBounds_ReturnsTypeDefault()
130+
{
131+
// Regression test for https://github.com/dotnet/runtime/issues/119834
132+
// ElementAtOrDefault should return default(T) for out-of-bounds indices,
133+
// not the DefaultIfEmpty default value
134+
135+
// Test with empty source
136+
int[] empty = [];
137+
var defaultIfEmpty = empty.DefaultIfEmpty(999);
138+
139+
// Index 0 should return the DefaultIfEmpty value (999)
140+
Assert.Equal(999, defaultIfEmpty.ElementAtOrDefault(0));
141+
142+
// Out-of-bounds indices should return default(int) which is 0, not 999
143+
Assert.Equal(0, defaultIfEmpty.ElementAtOrDefault(1));
144+
Assert.Equal(0, defaultIfEmpty.ElementAtOrDefault(2));
145+
Assert.Equal(0, defaultIfEmpty.ElementAtOrDefault(-1));
146+
147+
// Test with different type (string)
148+
string[] emptyStrings = [];
149+
var defaultIfEmptyString = emptyStrings.DefaultIfEmpty("default");
150+
151+
// Index 0 should return the DefaultIfEmpty value
152+
Assert.Equal("default", defaultIfEmptyString.ElementAtOrDefault(0));
153+
154+
// Out-of-bounds indices should return default(string) which is null
155+
Assert.Null(defaultIfEmptyString.ElementAtOrDefault(1));
156+
Assert.Null(defaultIfEmptyString.ElementAtOrDefault(2));
157+
}
127158
}
128159
}

0 commit comments

Comments
 (0)