- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个特殊的方法,偶尔会因 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,List<>.AddRange() 应根据需要自动调整自身大小:
If the new Count (the current Count plus the size of the collection) will be greater than Capacity, the capacity of the List<(Of <(T>)>) is increased by automatically reallocating the internal array to accommodate the new elements, and the existing elements are copied to the new array before the new elements are added.
有人能想到 AddRange 会抛出此类异常的情况吗?
编辑:
回答有关 FindObjects() 方法的问题。它基本上看起来像这样:
List<MyObject> retObjs = new List<MyObject>();
foreach(MyObject obj in objectList)
{
if(someCondition)
retObj.Add(obj);
}
最佳答案
您是否尝试从多个线程更新同一个列表?这可能会导致问题... List<T>
对于多个作者来说是不安全的。
关于c# - LIST<> AddRange 抛出 ArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2593267/
我有一个存储过程,我正在尝试使用 Dapper 执行该过程,该过程引发了一个错误,该错误似乎与我正在尝试做的事情无关,尽管我似乎无法弄清楚我在做什么错误的。 这是我试图调用的存储过程的签名: ALTE
我想匹配浏览器游戏中的坐标。我的正则表达式是: try { Regex r = new Regex("Mond ([1-9]) \x5B([1-
任何人都可以向我解释为什么我从这段代码中收到 ArgumentException 吗?它告诉我 Task.WhenAll 函数中的 tasks 参数包含空值。但是,它只是在我运行它时偶尔发生一次。 s
我正在尝试为 Eddystone 信标做广告,但我的代码在 advertisementData.Data 处失败并出现 ArgumentException: Value does not fall w
我已经编写了一些代码来从我的 Blogger 博客中导入内容。下载完所有 HTML 内容后,我将浏览图像标签并下载相应的图像。在大量情况下,System.Drawing.Bitmap.FromStre
我正在尝试更加熟悉表达式树。 我创建了一个简单的 switch 表达式,如下所示: var paramExp = Expression.Parameter(typeof(int)); Expressi
我正在尝试使用任务并行库逐个单元地构建矩阵。 我有以下代码可以执行此操作: List campaigns = GetHomeCampaigns(); Dictionary sellers = GetH
我试图通过添加三个参数来调用存储过程,但在添加第二个参数时出现以下 ArgumentException The SqlParameter is already contained by another
因为 CultureInfo 没有从一个线程复制到另一个线程,所以我使用了以下方法来为我做这件事。 public static StartCustomTask(Action action, TaskC
在我的代码中,我怎样才能显示弹出的警告框?我试图重播抛出新的参数异常并添加 Response.Write("alert('NOT Successful');");但它表明我在附近有语法错误”)”。如果
我有一个通过串行端口连接到设备的实用程序。为了测试这个应用程序,我正在为该设备编写一个模拟器。 这两个应用程序旨在在同一台 Windows 7 机器上运行。 我正在用 C# 编写模拟器。我使用 com
据我所知,ArgumentExceptions 通常是这样使用的: public void UpdateUser(User user) { if (user == null) throw ne
我有一个采用三个参数(3D 点)的方法。如果这些点是共线的,我想抛出一个异常。对我来说明显的异常(exception)是 ArgumentException,但最好的做法是在构造函数中包含参数名称。在
我正在使用 Path.Combine , 并且其中一个字符串包含 Unicode 字符。我收到 {System.ArgumentException} exception; illegal charac
当我尝试合并两个字典时,我不知道如何将键和值保留在字典中。由于 key 重复,我不断收到 ArgumentException。当键匹配时,我只想通过 =+ kvp.value; 添加值 我有一个字典列
当构造一个 ArgumentException 时,一些重载采用一个字符串作为无效参数的参数名称。我认为每当我更改方法的参数名称时不必记住更新此 ctor 参数会很好。有没有一种简单的方法可以使用反射
当我尝试合并两个字典时,我不知道如何将键和值保留在字典中。由于 key 重复,我不断收到 ArgumentException。当键匹配时,我只想通过 =+ kvp.value; 添加值 我有一个字典列
我想使用XamarinUITest在Xamarin上测试IOS应用程序。 不幸的是,我无法在本地设备上启动任何东西。 我已成功在Android上进行测试,但IOS似乎是一个全新的谜。 其实我有一个错误
我们的 SonarQube 经常在我们的代码中提出以下问题(代码异味):“用于 ArgumentException 的参数名称应与现有参数名称匹配”。 Here是触发此问题的规则。 触发此问题的示例如
在安装 WebActivatorEx nuget 包后尝试访问页面时,我不断收到以下 ArgumentException。 Server Error in '/PharmaDotnet/ux' App
我是一名优秀的程序员,十分优秀!