Skip to content

ImmutableArray<T>.IndexOf throws ArgumentOutOfRangeException when startIndex equals Length and count is 0 #124968

Closed

Description

@prozolic

Description

In ImmutableArray<T>.IndexOf, the upper bound guard for startIndex is startIndex < self.Length,
whereas Array.IndexOf is implemented with startIndex <= array.Length.
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Array.cs#L1904-L1907

As a result, when startIndex == ImmutableArray.Length and count == 0,
Array.IndexOf returns -1, but ImmutableArray<T>.IndexOf throws an ArgumentOutOfRangeException.

Reproduction Steps

int[] array = [1, 2, 3, 4];
var index = Array.IndexOf(array, 2, 4, 0); // Return -1

var immutableArray = ImmutableArray.Create(1, 2, 3, 4);
var index = immutableArray.IndexOf(2, 4, 0); // throw ArgumentOutOfRangeException (should return -1)

Expected behavior

-1 is returned.

Actual behavior

ArgumentOutOfRangeException is thrown.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

When we investigated #70950, I noticed this issue.
Additionally, ImmutableList<T>.IndexOf also had issues such as a missing upper bound guard,
so a PR addressing these together has been submitted at #124967.

Activity

  1. added
    untriaged
    on Feb 27, 2026
  2. dotnet-policy-service commented on Feb 27, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-collections
    See info in area-owners.md if you want to be subscribed.

  3. added 3 commits that reference this issue on Mar 29, 2026
    ca5957d
    f2d6f31
    60a9f69
  4. added this to the 11.0.0 milestone on Apr 19, 2026
  5. removed
    untriaged
    on Apr 19, 2026
  6. locked and limited conversation to collaborators on May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions