C# 类型为';的未处理异常;System.ArgumentOutOfRangeException';发生在mscorlib.dll中
在下面的代码中,我得到了以下错误 mscorlib.dll中发生类型为“System.ArgumentOutOfRangeException”的未处理异常 附加信息:索引超出范围。必须为非负数且小于集合的大小 以下是代码:C# 类型为';的未处理异常;System.ArgumentOutOfRangeException';发生在mscorlib.dll中,c#,exception,arguments,C#,Exception,Arguments,在下面的代码中,我得到了以下错误 mscorlib.dll中发生类型为“System.ArgumentOutOfRangeException”的未处理异常 附加信息:索引超出范围。必须为非负数且小于集合的大小 以下是代码: public ProcessInformation GetMaxRunTimeForApplicationsBetween(DateTime StartingTime, DateTime EndingTime) { //Filter Based on
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);
})];
}
public ProcessInformation GetMaxRunTimeForApplicationsBetween(日期时间开始时间、日期时间结束时间)
{
//基于定时器的滤波器
列表过滤器列表=新列表();
foreach(此.ProcessList中的HarvestApp.ProcessInformation项)
{
if(item.started_在.CompareTo(StartingTime)>=0和&item.end_在.CompareTo(EndingTime)max)
{
最大值=项目;
}
}
//返回applicationNameList对应的ProcessInformation类
return filterList[filterList.FindIndex(委托
(处理信息程序)
{
return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)],StringComparison.Ordinal);
})];
}
问题出在这里:
if (applicationNameList.Contains(item.name))
{
**countApplicationList[applicationNameList.IndexOf(item.name)]++;**
}
应该是这样的
if (applicationNameList.Contains(item.name) && countApplicationList.Count > applicationNameList.IndexOf(item.name))
{
countApplicationList[applicationNameList.IndexOf(item.name)]++;
}
我认为这里有一条错误线:
return filterList[filterList.FindIndex(delegate(ProcessInformation proc)
{
return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
})];
因为当您找不到索引时,List.FindIndex
可以返回-1
相反,在使用索引之前,您应该测试索引是否小于0,这表明存在错误:
int result = filterList.FindIndex(delegate(ProcessInformation proc)
{
return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
});
if(result < 0) throw new Exception("Cant't Find ProcessInformation");
return filterList[result];
int result=filterList.FindIndex(委托(ProcessInformation proc)
{
return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)],StringComparison.Ordinal);
});
如果(结果<0)抛出新异常(“找不到ProcessInformation”);
返回过滤器列表[结果];
根据异常名称和代码样式猜测C#,如果不是,请使用适当的语言标记重新标记。此异常可能发生在此代码中的多个位置。这是哪条线路的故障?你知道的很多。return语句会发生异常。stacktrace呢?我试过了。但还是给了我同样的例外。它在返回语句中显示问题。是。返回语句中出现错误。