diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs index 4de80f12537927..7efb68bfc658bb 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs @@ -793,12 +793,15 @@ public int IndexOf(T item, int startIndex, int count) /// The 0-based index into the array where the item was found; or -1 if it could not be found. public int IndexOf(T item, int startIndex, int count, IEqualityComparer? equalityComparer) { - if (count == 0 && startIndex == 0) + if (count == 0) { - return -1; + if (startIndex == 0 || startIndex == this.Count) + { + return -1; + } } - Requires.Range(startIndex >= 0 && startIndex < this.Count, nameof(startIndex)); + Requires.Range(startIndex >= 0 && startIndex <= this.Count, nameof(startIndex)); Requires.Range(count >= 0 && startIndex + count <= this.Count, nameof(count)); equalityComparer ??= EqualityComparer.Default; diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs index ec4d94dc62184e..5dc90a900bc6c0 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs @@ -168,12 +168,15 @@ public int IndexOf(T item, int startIndex, int count, IEqualityComparer? equa ImmutableArray self = this; self.ThrowNullRefIfNotInitialized(); - if (count == 0 && startIndex == 0) + if (count == 0) { - return -1; + if (startIndex == 0 || startIndex == self.Length) + { + return -1; + } } - Requires.Range(startIndex >= 0 && startIndex < self.Length, nameof(startIndex)); + Requires.Range(startIndex >= 0 && startIndex <= self.Length, nameof(startIndex)); Requires.Range(count >= 0 && startIndex + count <= self.Length, nameof(count)); equalityComparer ??= EqualityComparer.Default; @@ -253,12 +256,15 @@ public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer? ImmutableArray self = this; self.ThrowNullRefIfNotInitialized(); - if (startIndex == 0 && count == 0) + if (count == 0) { - return -1; + if (startIndex == 0 || startIndex == self.Length) + { + return -1; + } } - Requires.Range(startIndex >= 0 && startIndex < self.Length, nameof(startIndex)); + Requires.Range(startIndex >= 0 && startIndex <= self.Length, nameof(startIndex)); Requires.Range(count >= 0 && startIndex - count + 1 >= 0, nameof(count)); equalityComparer ??= EqualityComparer.Default; diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs index 80a3ec1a37e6f0..6d8f2b83510033 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs @@ -2855,5 +2855,28 @@ public StructWithReferenceTypeField(string foo) this.foo = foo; } } + + [Theory] + [InlineData(0, 0)] // startIndex 0, count 0 + [InlineData(4, 0)] // startIndex == Length, count 0 + [InlineData(2, 0)] // intermediate startIndex, count 0 + public void IndexOf_CountZero_ReturnsMinusOne(int startIndex, int count) + { + var array = ImmutableArray.Create(1, 2, 3, 4); + Assert.Equal(-1, array.IndexOf(2, startIndex, count)); + } + + [Fact] + public void IndexOf_EmptyArray_CountZero_ReturnsMinusOne() + { + Assert.Equal(-1, ImmutableArray.Empty.IndexOf(1, 0, 0)); + } + + [Fact] + public void IndexOf_Builder_CountZero_ReturnsMinusOne() + { + var builder = ImmutableArray.Create(1, 2, 3, 4).ToBuilder(); + Assert.Equal(-1, builder.IndexOf(2, builder.Count, 0)); + } } }