I'm hitting an exception with a List in C# when I add an item in the list. I can't really see anything specific there.
tempList.ForEach(tempDis => alloc.ListDistribution.Add(tempDis));
When I hover
alloc.ListDistribution, the debugger tells me there is 6 items in the list. If I try to expand those items, I have an exception that says:
Source array was not long enough. Check srcIndex and length, and the array's lower bounds. at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) at System.Collections.Generic.List
1.CopyTo(T array, Int32 arrayIndex) at System.Collections.Generic.Mscorlib_CollectionDebugView1.get_Items()
And I can see the Capacity of the list is 4, while the Count is 6. So the exception is triggered while the system is inceasing capacity.
While that makes no sense to me, the documentation of the mentions in the list of Exception that there is a scenario where:
ArgumentOutOfRangeException Capacity is set to a value that is less than Count.
But in the remarks specifies this can't happen: Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.
Any idea what could cause this exception to be triggered?</div