【发布时间】:2013-06-10 12:35:33
【问题描述】:
在下面的代码中,我得到了以下错误。
在 mscorlib.dll 中发生了“System.ArgumentOutOfRangeException”类型的未处理异常
附加信息:索引超出范围。必须为非负数且小于集合的大小。
代码如下:
public ProcessInformation GetMaxRunTimeForApplicationsBetween(DateTime StartingTime, DateTime EndingTime)
{
//Filter Based on Timer
List<ProcessInformation> filterList = new List<ProcessInformation>();
foreach (HarvestApp.ProcessInformation item in this.ProcessList)
{
if (item.started_at.CompareTo(StartingTime) >= 0 && item.ended_at.CompareTo(EndingTime) <= 0)
{
filterList.Add(item);
}
}
//Count Max Occurrence of Application
List<int> countApplicationList = new List<int>();
List<string> applicationNameList = new List<string>();
foreach (HarvestApp.ProcessInformation item in filterList)
{
if (applicationNameList.Contains(item.name))
{
countApplicationList[applicationNameList.IndexOf(item.name)]++;
}
else
{
applicationNameList.Add(item.name);
countApplicationList.Add(1);
}
}
//if (countApplicationList.Count == 0)
//{
// throw new InvalidOperationException("Empty list");
//}
int max = int.MinValue;
foreach (int item in countApplicationList)
{
if (item > max)
{
max = item;
}
}
//Return corresponding ProcessInformation Class of applicationNameList
return filterList[filterList.FindIndex(delegate
(ProcessInformation proc)
{
return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
})];
}
【问题讨论】:
-
从异常名称和代码风格猜测C#,如果没有,请用适当的语言标签重新标记。
-
此异常可能发生在此代码中的多个地方。它在哪条线上失败了?你知道的太多了。
-
return 语句出现异常。
-
堆栈跟踪呢?