C# mscorlib.dll 中发生类型为“System.ArgumentOutOfRangeException”的未处理异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17024040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:23:19  来源:igfitidea点击:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

c#exceptionarguments

提问by user2413158

In the following code, I got following error.

在以下代码中,我收到以下错误。

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

mscorlib.dll 中发生类型为“System.ArgumentOutOfRangeException”的未处理异常

附加信息:索引超出范围。必须是非负的并且小于集合的大小。

Here is code:

这是代码:

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);
                })];




    }

回答by Venu Vedula

Here is the problem:

这是问题所在:

if (applicationNameList.Contains(item.name))
{
       **countApplicationList[applicationNameList.IndexOf(item.name)]++;**
}

It should be like this

应该是这样的

if (applicationNameList.Contains(item.name) && countApplicationList.Count > applicationNameList.IndexOf(item.name))
    {
       countApplicationList[applicationNameList.IndexOf(item.name)]++;
    }

回答by ???

I think error line is here:

我认为错误行在这里:

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

Because List<T>.FindIndexcan return -1when you can't find index.

因为找不到索引时List<T>.FindIndex可以返回-1

Instead you should test if the index is less than 0, which indicates there is an error, before you use it:

相反,您应该在使用它之前测试索引是否小于 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];