Strange exception when using ConcurrentStack in C #

I have C # code, it throws an ArgumentOutOfRangeException , I wonder why?

  ConcurrentStack<int> intsStack = new ConcurrentStack<int>(); int[] myInts = new int[0]; intsStack.PushRange(myInts); 

Error Message property ArgumentOutOfRangeException :

The startIndex argument must be greater than or equal to zero.

Parameter Name: startIndex

The array is empty, but not null , I did not expect any exception at all, just nothing was added to the stack. Is this a reasonable exception or not?

+7
source share
3 answers

The AddRange argument is an empty array, so you are trying to push null elements onto the stack. Therefore, this is a very reasonable exception.

EDIT: But you're right, this is a bad exception message. You can guess that this is due to the fact that the PushRange internal overload (T [], Int32, Int32) actually throws an exception.

+3
source

From an implementation point of view , it makes sense if you look at the definition of PushRange (T [], int, int) overload :

ArgumentOutOfRangeException: startIndex or count is negative. Or startIndex is greater than or equal to the length of the elements.

The length of your array is zero. Therefore, there is no valid value for startIndex.

From the point of view of the documentation, this is not reasonable, because the documentation of PushRange (T []) overloads does not mention ArgumentOutOfRangeException .

+3
source

If the array is empty, it does not have index 0, which it complains about.

With an index with a null value, 0 is the first index pointing to the first element. Therefore, in fact, he complains that there is nothing in the array.

+2
source

All Articles