diff --git a/src/StructLinq.Tests/FirstOrDefaultTests.cs b/src/StructLinq.Tests/FirstOrDefaultTests.cs index ae6547bb..d15ffcfd 100644 --- a/src/StructLinq.Tests/FirstOrDefaultTests.cs +++ b/src/StructLinq.Tests/FirstOrDefaultTests.cs @@ -40,7 +40,6 @@ public void ShouldReturnDefaultZeroAlloc() StructEnumerable.Empty().FirstOrDefault(x=>x).Should().Be(default); } - [Fact] public void ShouldReturnFirstElementWithFunc() { @@ -62,5 +61,16 @@ public void ShouldReturnFirstElementWithFuncZeroAlloc() .Should() .Be(6); } + + [Fact] + public void ShouldReturnDefaultValue() + { + var array = Enumerable.Range(0, 10) + .ToArray() + .ToStructEnumerable() + .FirstOrDefault(x => x == 11) + .Should() + .Be(default); + } } } diff --git a/src/StructLinq/First/StructCollection.First.cs b/src/StructLinq/First/StructCollection.First.cs index c55a7e62..313cec2e 100644 --- a/src/StructLinq/First/StructCollection.First.cs +++ b/src/StructLinq/First/StructCollection.First.cs @@ -105,9 +105,12 @@ public static bool TryFirst(this TCollection collec return false; for (int i = 0; i < collection.Count; i++) { - first = collection.Get(i); - if (predicate(first)) + var current = collection.Get(i); + if (predicate(current)) + { + first = current; return true; + } } return false; } @@ -120,9 +123,12 @@ public static bool TryFirst(this IStructCollection(this TCollection return false; for (int i = 0; i < collection.Count; i++) { - first = collection.Get(i); + var current = collection.Get(i); if (predicate.Eval(first)) + { + first = current; return true; + } } return false; }