我有一个特定的方法是偶尔用一个ArgumentException崩溃:LIST <>的AddRange抛出ArgumentException的
Destination array was not long enough. Check destIndex 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.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
导致此崩溃的代码看起来是这样的:
List<MyType> objects = new List<MyType>(100);
objects = FindObjects(someParam);
objects.AddRange(FindObjects(someOtherParam);
根据MSDN,列表<> .AddRange()应根据需要自动调整其大小:
如果新计数(当前计数加上集合的大小)将大于容量,则通过自动重新分配内部数组以适应新元素来增加列表<(Of> <(T>)>)的容量,并且在添加新元素之前将现有元素复制到新数组中。
有人能想到AddRange可以抛出这种异常的情况吗?
编辑:
响应于关于FindObjects问题()方法。它基本上看起来像这样:
List<MyObject> retObjs = new List<MyObject>();
foreach(MyObject obj in objectList)
{
if(someCondition)
retObj.Add(obj);
}
FindObjects返回什么?另外,为什么初始化'objects'然后及时在下一行重新分配它? – 2010-04-07 14:31:38
当您添加一个项目时,它会检查大小是否足够大,如果不是,则调整它使用的内部数组大小。然而,当使用多线程时,可以检查,获取false并调整数组大小,下一个线程读取的是足够的空间并且不调整大小,然后它们都会到达实际的'this._items [this._size ++] = item ;'代码在同一时间...导致后者线程爆炸。当Jon说'List'在下面的答案中不是线程安全的,这是'List '不能处理的事情之一。 –
2010-04-07 14:39:52
我没有写代码,我只是在调试这个问题。有很多奇怪的东西,维护人员在翻找时发现:) – Tim 2010-04-07 14:41:07