Open krymtkts opened 3 weeks ago
Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.
Seems intended: dotnet/runtime#62126
Transferred this over to be an API docs issue to be fixed in xml/System.Collections.Concurrent/ConcurrentStack`1.xml. We should also update ConcurrentStack.cs (dotnet/runtime) to keep source and docs aligned. We just need appropriately clear wording to capture this nuance.
We should check PushRange
's behavior to see if it also needs to be updated.
Marked as https://github.com/dotnet/dotnet-api-docs/labels/help%20wanted
Description
ConcurrentStack<T>.TryPopRange
method does not throw anArgumentOutOfRangeException
whenstartIndex
is equal to the length ofitems
array. The documentation is as follows.ConcurrentStack.TryPopRange Method (System.Collections.Concurrent) | Microsoft Learn >)
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 ofitems
.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).
Expected behavior
According to the documentation,
ArgumentOutOfRangeException
should be thrown whenstartIndex
is equal to the length ofitems
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.
Known Workarounds
Check
startIndex
and the length ofitems
manually before callingTryPopRange
.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 argumentcount
is 0 beforeValidatePushPopRangeInput
.https://github.com/dotnet/runtime/blob/48dbc4fc836da67cf1efb6b348499a918c4dea8e/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentStack.cs#L533-L548