Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List<T>.IndexOf outputs incorrect error message #67423

Open
HeinziAT opened this issue Apr 1, 2022 · 1 comment
Open

List<T>.IndexOf outputs incorrect error message #67423

HeinziAT opened this issue Apr 1, 2022 · 1 comment
Labels
area-System.Collections untriaged

Comments

@HeinziAT
Copy link

@HeinziAT HeinziAT commented Apr 1, 2022

Description

The error message returned by List<T>.IndexOf if an invalid index is used is incorrect. As it is currently implemented, the size of the collection (2 in the repro example below) is a valid input for index.

Reproduction Steps

// list of length 2
var list = new List<string>() { "a", "b" };

Console.WriteLine(list.IndexOf("a", 2)); // yields -1
Console.WriteLine(list.IndexOf("a", 3)); // yields an error

(fiddle)

Expected behavior

An ArgumentException with the following error text is thrown:

Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'index')

(Alternatively: Must be non-negative and not greater than the size of the collection.)

Actual behavior

An ArgumentException with the following error text is thrown:

Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Regression?

No response

Known Workarounds

No response

Configuration

Tested with dotnetfiddle, with all currently available compilers (.NET 4.7.2, Roslyn 4.0, .NET 6).

Other information

I do not suggest to change the behavior of IndexOf - the current behavior is the only sane and consistent one. However, the error message (and the documentation of List<T>.IndexOf) should be updated to reflect the actual behavior.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Collections untriaged labels Apr 1, 2022
@msftbot
Copy link
Contributor

@msftbot msftbot bot commented Apr 1, 2022

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

Issue Details

Description

The error message returned by List<T>.IndexOf if an invalid index is used is incorrect. As it is currently implemented, the size of the collection (2 in the repro example below) is a valid input for index.

Reproduction Steps

// list of length 2
var list = new List<string>() { "a", "b" };

Console.WriteLine(list.IndexOf("a", 2)); // yields -1
Console.WriteLine(list.IndexOf("a", 3)); // yields an error

(fiddle)

Expected behavior

An ArgumentException with the following error text is thrown:

Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'index')

(Alternatively: Must be non-negative and not greater than the size of the collection.)

Actual behavior

An ArgumentException with the following error text is thrown:

Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Regression?

No response

Known Workarounds

No response

Configuration

Tested with dotnetfiddle, with all currently available compilers (.NET 4.7.2, Roslyn 4.0, .NET 6).

Other information

I do not suggest to change the behavior of IndexOf - the current behavior is the only sane and consistent one. However, the error message (and the documentation of List<T>.IndexOf) should be updated to reflect the actual behavior.

Author: HeinziAT
Assignees: -
Labels:

area-System.Collections, untriaged

Milestone: -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Collections untriaged
Projects
None yet
Development

No branches or pull requests

1 participant