【问题标题】:An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dllmscorlib.dll 中出现“System.ArgumentOutOfRangeException”类型的未处理异常
【发布时间】: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 语句出现异常。
  • 堆栈跟踪呢?

标签: c# exception arguments


【解决方案1】:

我认为错误行在这里:

return filterList[filterList.FindIndex(delegate(ProcessInformation proc)
    {
        return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
    })];

因为List&lt;T&gt;.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];

【讨论】:

  • 是的。 return 语句出错。
【解决方案2】:

问题来了:

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 语句中显示问题。
猜你喜欢
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多