dotnet / dotnet/dotnet-api-docs
`ConcurrentStack<T>.TryPopRange` does not throw `ArgumentOutOfRangeException` when `startIndex` equals the length of `items`
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
### Description
`ConcurrentStack.TryPopRange` method does not throw an `ArgumentOutOfRangeException` when `startIndex` is equal to the length of `items` array.
The documentation is as follows.
[ConcurrentStack.TryPopRange Method (System.Collections.Concurrent) | Microsoft Learn]()
> ArgumentOutOfRangeException
> startIndex or count is negative. Or startIndex is greater than or equal to the length of items.
This behavior is observed in .NET 8(PowerShell 7.4) but not in .NET Framework 4.5(Windows PowerShell 5.1), where the exception is correctly thrown.
.NET Framework checks whether the `startIndex` is equal to the length of `items`.
https://github.com/microsoft/referencesource/blob/51cf7850defa8a17d815b4700b67116e3fa283c2/mscorlib/system/collections/Concurrent/ConcurrentStack.cs#L473C1-L492
.NET doesn't.
https://github.com/dotnet/runtime/blob/48dbc4fc836da67cf1efb6b348499a918c4dea8e/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentStack.cs#L390-L404
### Reproduction Steps
Run the following code in PowerShell 7.4(.NET 8).
```powershell
$s = [System.Collections.Concurrent.ConcurrentStack[System.String]]::new()
$a = [string[]]::new(0)
$s.TryPopRange($a) # TryPopRange(T[] items) calls TryPopRange(items, 0, items.Length)
$s.Push("a")
$b = [string[]]::new(1)
$s.TryPopRange($b, 1, 0)
```
### Expected behavior
According to the documentation, `ArgumentOutOfRangeException` should be thrown when `startIndex` is equal to the length of `items` array.
### Actual behavior
No exception is thrown.
### Regression?
Yes, this behavior seems to be a regression from [Use ArgumentOutOfRangeException.Throw helpers in more places (#79460) · dotnet/runtime@3689fbe](https://github.com/dotnet/runtime/commit/3689fbec921418e496962dc0ee252bdc9eafa3de).
### Known Workarounds
Check `startIndex` and the length of `items` manually before calling `TryPopRange`.
### Configuration
.NET 8.0.401
### Other information
I noticed this issue when I passed an empty array to `TryPopRange`.
Although it doesn't match the documentation, the current behavior of not throwing an error when passing an empty array is convenient.
It might be better to implement an early return if the third argument `count` is 0 before `ValidatePushPopRangeInput`.
https://github.com/dotnet/runtime/blob/48dbc4fc836da67cf1efb6b348499a918c4dea8e/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentStack.cs#L533-L548
Contributor guide
Assessment
This issue has not been assessed yet.