-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsDescriptionThe error message returned by Reproduction Steps
(fiddle) Expected behaviorAn 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 behaviorAn 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 WorkaroundsNo response ConfigurationTested with dotnetfiddle, with all currently available compilers (.NET 4.7.2, Roslyn 4.0, .NET 6). Other informationI do not suggest to change the behavior of
|
From what I see we are pretty much inconsistent in usage of
Of course we also should not forget to update docs. I would be grateful if someone from area owners, perhaps @jeffhandley, answered:
|
Naive guess: it's implemented this way to support striding/slicing: int previousIndex = 0;
int index = commaDelimitedString.IndexOf(",", 0);
while(index > -1)
{
handleElement(commaDelimitedString.Substring(previousIndex, index));
previousIndex = index;
index = commaDelimitedString.IndexOf(",", index + 1);
} ...also, consider the case when the string is empty. |
@B1Z0N happy to accept a PR that improves this. |
Separated "Must be less than the size" and "Must be less than or equal to the size" error messages. Now first one goes in "throw on index >= size case" and the second goes in "throw on index > size". Fix dotnet#67423
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 forindex
.Reproduction Steps
(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 ofList<T>.IndexOf
) should be updated to reflect the actual behavior.The text was updated successfully, but these errors were encountered: