GVKun编程网logo

什么是IndexOutOfRangeException / ArgumentOutOfRangeException,如何解决?(indexoutofrangeexception异常)

6

在本文中,您将会了解到关于什么是IndexOutOfRangeException/ArgumentOutOfRangeException,如何解决?的新资讯,同时我们还将为您解释indexoutofr

在本文中,您将会了解到关于什么是IndexOutOfRangeException / ArgumentOutOfRangeException,如何解决?的新资讯,同时我们还将为您解释indexoutofrangeexception异常的相关在本文中,我们将带你探索什么是IndexOutOfRangeException / ArgumentOutOfRangeException,如何解决?的奥秘,分析indexoutofrangeexception异常的特点,并给出一些关于ArgumentOutOfRangeException:索引和长度必须引用字符串中的某个位置、arraylist OutOfRangeException 上的异常、asp.net – 将MemoryCache与HostFileChangeMonitor init一起使用到目录获取ArgumentOutOfRangeException、C# DirectorySearcher.FindOne();得到 thores ArgumentOutOfRangeException的实用技巧。

本文目录一览:

什么是IndexOutOfRangeException / ArgumentOutOfRangeException,如何解决?(indexoutofrangeexception异常)

什么是IndexOutOfRangeException / ArgumentOutOfRangeException,如何解决?(indexoutofrangeexception异常)

我有一些代码,当它执行时,它抛出一个IndexOutOfRangeException,说:

指数数组的边界之外。

这是什么意思,我该怎么办?

根据使用的类,它也可以是 ArgumentOutOfRangeException

mscorlib.dll中发生类型’System.ArgumentOutOfRangeException’的异常,但未在用户代码中处理。其他信息:索引超出范围。必须为非负数并且小于集合的大小。

答案1

小编典典

它是什么?

此异常表示您正在尝试使用无效索引按索引访问集合项。当索引小于集合的下限或大于或等于其包含的元素数时,索引无效。

当它被扔

给定一个声明为的数组:

byte[] array = new byte[4];

您可以从0到3访问此数组,超出此范围的值将导致IndexOutOfRangeException抛出该数组。创建和访问数组时,请记住这一点。

数组长度
在C#中,数组通常基于0。这意味着第一个元素的索引为0,最后一个元素的索引为Length -1Length数组中的项目总数),因此此代码不起作用:

array[array.Length] = 0;

此外,请注意,如果您具有多维数组,则不能同时使用Array.Length两个维度,而必须使用Array.GetLength()

int[,] data = new int[10, 5];for (int i=0; i < data.GetLength(0); ++i) {    for (int j=0; j < data.GetLength(1); ++j) {        data[i, j] = 1;    }}

上限不包含在内
在以下示例中,我们创建的原始二维数组Color。每个项目代表一个像素,索引从(0, 0)(imageWidth - 1, imageHeight- 1)

Color[,] pixels = new Color[imageWidth, imageHeight];for (int x = 0; x <= imageWidth; ++x) {    for (int y = 0; y <= imageHeight; ++y) {        pixels[x, y] = backgroundColor;    }}

然后此代码将失败,因为数组是基于0的,并且图像中的最后一个(右下)像素为pixels[imageWidth - 1, imageHeight - 1]

pixels[imageWidth, imageHeight] = Color.Black;

在另一种情况下,您可能会获得ArgumentOutOfRangeException此代码(例如,如果您在类GetPixel上使用method
Bitmap)。

数组不增长
数组很快。与所有其他集合相比,线性搜索非常快。这是因为项目在内存中是连续的,所以可以计算内存地址(增量只是一个加法)。无需遵循节点列表,简单的数学运算!您为此付出了一定的限制:它们无法增长,如果您需要更多的元素,则需要重新分配该数组(如果必须将旧项目复制到新块中,这可能会花费相对较长的时间)。使用调整它们的大小Array.Resize<T>(),此示例将一个新条目添加到现有数组:

Array.Resize(ref array, array.Length + 1);

别忘了有效索引是从0Length -1。如果您只是简单地尝试分配一个项目就Length可以了IndexOutOfRangeException(如果您认为这些项目可能会以类似于Insert其他集合方法的语法增加,那么这种行为可能会使您感到困惑)。

__具有自定义下界的 特殊 数组数组中的
第一项始终具有索引0 。这并不总是正确的,因为您可以创建具有自定义下限的数组:

var array = Array.CreateInstance(typeof(byte), new int[] { 4 }, new int[] { 1 });

在该示例中,数组索引的有效范围是1到4。当然,上限不能更改。

错误的参数
如果您使用未经验证的参数(从用户输入或函数用户)访问数组,则可能会出现以下错误:

private static string[] RomanNumbers =    new string[] { "I", "II", "III", "IV", "V" };public static string Romanize(int number){    return RomanNumbers[number];}

意外的结果
也可能由于另一个原因引发此异常:按照惯例,许多 搜索函数 将返回-1(在.NET
2.0中引入了nullable,无论如何它也是多年来使用的众所周知的惯例)。什么也找不到。假设您有一个与字符串可比的对象数组。您可能会想编写以下代码:

// Items comparable with a stringConsole.WriteLine("First item equals to ''Debug'' is ''{0}''.",    myArray[Array.IndexOf(myArray, "Debug")]);// Arbitrary objectsConsole.WriteLine("First item equals to ''Debug'' is ''{0}''.",    myArray[Array.FindIndex(myArray, x => x.Type == "Debug")]);

如果其中没有项目myArray将满足搜索条件Array.IndexOf(),则此操作将失败,因为它将返回-1,然后将引发数组访问。

下一个示例是一个幼稚的示例,用于计算给定的一组数字(知道最大数字并返回一个数组,其中索引0的项表示数字0,索引1的项表示数字1,依此类推):

static int[] CountOccurences(int maximum, IEnumerable<int> numbers) {    int[] result = new int[maximum + 1]; // Includes 0    foreach (int number in numbers)        ++result[number];    return result;}

当然,这是一个非常糟糕的实现,但是我想展示的是它会因为负数和上面的数字而失败maximum

它如何适用List<T>

与数组相同-有效索引范围-0(List索引始终以0开头)到list.Count-访问此范围之外的元素将导致异常。

请注意,在数组使用的相同情况下List<T>会抛出该异常。ArgumentOutOfRangeException``IndexOutOfRangeException

与数组不同,List<T>开始是空的-因此尝试访问刚创建的列表的项目会导致此异常。

var list = new List<int>();

常见的情况是使用索引编制列表(类似于Dictionary<int, T>)会导致异常:

list[0] = 42; // exceptionlist.Add(42); // correct

IDataReader和Columns
假设您正在尝试使用以下代码从数据库读取数据:

using (var connection = CreateConnection()) {    using (var command = connection.CreateCommand()) {        command.CommandText = "SELECT MyColumn1, MyColumn2 FROM MyTable";        using (var reader = command.ExecuteReader()) {            while (reader.Read()) {                ProcessData(reader.GetString(2)); // Throws!            }        }    }}

GetString()将会抛出异常,IndexOutOfRangeException因为您的数据集只有两列,但是您试图从第3列中获取一个值(索引
始终 基于0)。

请注意,此行为与大多数IDataReader实现(SqlDataReaderOleDbDataReader依此类推)相同。

如果使用索引器运算符的IDataReader重载(采用列名并传递无效的列名),也可以得到相同的异常。
例如,假设您检索了一个名为 Column1 的列,然后尝试使用以下方法检索该字段的值

 var data = dr["Colum1"];  // Missing the n in Column1.

发生这种情况是因为实现了索引器运算符,试图检索不存在的 Colum1 字段的索引。当其内部帮助程序代码返回-1作为“
Colum1”的索引时,GetOrdinal方法将引发此异常。

其他
当引发此异常时,还有另一种(记录在案)的情况:如果in DataView中提供给DataViewSort属性的数据列名称无效。

如何避免

在此示例中,为简单起见,让我假设数组始终是一维的且基于0。如果你想成为严格的(或者你正在开发一个库),你可能需要更换0GetLowerBound(0).LengthGetUpperBound(0)(当然,如果你有参数类型为System.ArraY,它并不适用于T[])。请注意,在这种情况下,上限包含以下代码:

for (int i=0; i < array.Length; ++i) { }

应该这样重写:

for (int i=array.GetLowerBound(0); i <= array.GetUpperBound(0); ++i) { }

请注意,这是不允许的(它将抛出InvalidCastException),这就是为什么如果您的参数T[]对自定义下限数组安全的原因:

void foo<T>(T[] array) { }void test() {    // This will throw InvalidCastException, cannot convert Int32[] to Int32[*]    foo((int)Array.CreateInstance(typeof(int), new int[] { 1 }, new int[] { 1 }));}

验证参数
如果索引来自参数,则应始终对其进行验证(抛出ArgumentExceptionArgumentOutOfRangeException)。在下一个示例中,错误的参数可能会导致IndexOutOfRangeException,此函数的用户可能会期望这样做,因为他们正在传递数组,但这并不总是那么明显。我建议始终验证公共功能的参数:

static void SetRange<T>(T[] array, int from, int length, Func<i, T> function){    if (from < 0 || from>= array.Length)        throw new ArgumentOutOfRangeException("from");    if (length < 0)        throw new ArgumentOutOfRangeException("length");    if (from + length > array.Length)        throw new ArgumentException("...");    for (int i=from; i < from + length; ++i)        array[i] = function(i);}

如果函数是私有的,则可以简单地将if逻辑替换为Debug.Assert()

Debug.Assert(from >= 0 && from < array.Length);

检查对象状态
数组索引可能不直接来自参数。它可能是对象状态的一部分。通常,验证对象状态(根据需要以及使用功能参数)始终是一种良好的做法。您可以使用Debug.Assert(),抛出适当的异常(对问题的更具描述性)或像下面的示例一样处理:

class Table {    public int SelectedIndex { get; set; }    public Row[] Rows { get; set; }    public Row SelectedRow {        get {            if (Rows == null)                throw new InvalidOperationException("...");            // No or wrong selection, here we just return null for            // this case (it may be the reason we use this property            // instead of direct access)            if (SelectedIndex < 0 || SelectedIndex >= Rows.Length)                return null;            return Rows[SelectedIndex];        }}

验证返回值
在前面的示例中,我们直接使用了Array.IndexOf()返回值。如果我们知道它可能会失败,那么最好处理这种情况:

int index = myArray[Array.IndexOf(myArray, "Debug");if (index != -1) { } else { }

如何调试

我认为,关于此错误的大多数问题都可以简单地避免。您花费在编写适当的问题上的时间(带有一个简短的示例和一个简短的解释)所花的时间可能比调试代码所花的时间要容易得多。首先,请阅读Eric
Lippert的有关调试小程序的博客文章,在这里我不会重复他的话,但这绝对是一本 必读的书

您具有源代码,具有堆栈跟踪的异常消息。去那里,选择正确的行号,您将看到:

array[index] = newValue;

您发现了错误,请检查如何index增加。这样对吗?检查数组如何分配,如何index增加连贯性?根据您的要求正确吗?如果您对所有这些问题的回答都是
肯定的 ,那么您将在StackOverflow上找到很好的帮助,但请先自己检查一下。您将节省自己的时间!

一个好的起点是始终使用断言并验证输入。您甚至可能想要使用代码合同。当出现问题时,您无法通过快速查看代码来判断会发生什么,那么您必须求助于老朋友:
debugger 。只需在Visual
Studio(或您最喜欢的IDE)中的调试中运行您的应用程序,您就会确切地看到哪一行引发此异常,涉及哪个数组以及您要使用哪个索引。确实,您有99%的时间会在几分钟内自行解决。

如果这在生产中发生,那么您最好在隐含代码中添加断言,可能我们不会在您的代码中看到您自己看不到的东西(但是您总是可以打赌)。

故事的VB.NET方面

我们在C#答案中说过的一切都对VB.NET有效,但语法上有明显的区别,但是在处理VB.NET数组时要考虑一个重要的观点。

在VB.NET中,声明数组以设置数组的最大有效索引值。它不是我们要存储在数组中的元素的数量。

'' declares an array with space for 5 integer '' 4 is the maximum valid index starting from 0 to 4Dim myArray(4) as Integer

因此,此循环将使用5个整数填充数组,而不会引起任何 IndexOutOfRangeException

For i As Integer = 0 To 4    myArray(i) = iNext

VB.NET规则

此异常表示您正在尝试使用无效索引按索引访问集合项。当索引小于集合的下限或大于集合的下限时,索引无效 等于它包含的元素数量。
数组声明中定义的最大允许索引

ArgumentOutOfRangeException:索引和长度必须引用字符串中的某个位置

ArgumentOutOfRangeException:索引和长度必须引用字符串中的某个位置

如何解决ArgumentOutOfRangeException:索引和长度必须引用字符串中的某个位置?

我的脚本有问题,如果有人能提供帮助,我将不胜感激。

问题来了:

ArgumentOutOfRangeException:索引和长度必须引用字符串中的某个位置。 参数名称:长度 System.String.Substring (system.int32 startIndex,system.int32 length) (at :0)

脚本是:

 public class ColorTypeConverter
 {
     public string ToRGBHex(Color c)
     {
         return string.Format("{0:X2}{1:X2}{2:X2}",ToByte(c.r),ToByte(c.g),ToByte(c.b));
     }
 
     private static byte ToByte(float f)
     {
         f = Mathf.Clamp01(f);
         return (byte)(f * 255);
     }
     
    private int HexToDec (string hex)
    {
        int dec = System.Convert.ToInt32(hex,16);
        return dec;
    }
    
    private float HexToFloatnormalized(string hex) {
        return HexToDec(hex) / 255f;
    }

    public Color GetColorFromString(string hexString) {
        float red = HexToFloatnormalized(hexString.Substring(0,2));
        float green = HexToFloatnormalized(hexString.Substring(2,2));
        float blue = HexToFloatnormalized(hexString.Substring(4,2));
        return new Color(red,green,blue);
    }
 }

UI 管理器脚本:

string s = PlayerPrefs.GetString("savecolorground");
ColorTypeConverter col = new ColorTypeConverter();
if(s != "")
    ground.GetComponent<SpriteRenderer>().color = col.GetColorFromString(s);
else
    ground.GetComponent<SpriteRenderer>().color = col.GetColorFromString("FFFFFF");

播放器脚本:

ColorTypeConverter colCon = new ColorTypeConverter();
string color = PlayerPrefs.GetString("savecolor");  
if(color != "")
    GetComponent<SpriteRenderer>().color = colCon.GetColorFromString(color);
else
    GetComponent<SpriteRenderer>().color = colCon.GetColorFromString("2AFCFF");

GetComponent<TrailRenderer>().startColor = new Color(
    GetComponent<SpriteRenderer>().color.r,GetComponent<SpriteRenderer>().color.g,GetComponent<SpriteRenderer>().color.b,255);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

arraylist OutOfRangeException 上的异常

arraylist OutOfRangeException 上的异常

如何解决arraylist OutOfRangeException 上的异常?

首先,我将一个 ArrayList 初始化到我的代码中,然后添加用户的每个输入。我设法添加了数据并显示,但这样做后它抛出了 OutOfRange 异常,我看不出任何原因。

我已经问过类似的问题,但它是为了检查数组是否已存储值 Link to my first question。但是我现在遇到的问题就是这个异常

static ArrayList fName = new ArrayList();
static ArrayList cNumber = new ArrayList();

public static void createContact()
{
    Console.Write("\nEnter your full name: ");
    fullName = Console.ReadLine();
    fName.Add(fullName);

    Console.Write("\nEnter your contact number: ");
    contactNumber = Console.ReadLine();
    cNumber.Add(contactNumber);
}

public static void displayContact()
{
    if (fName.Count == 0 && cNumber.Count == 0)
    {
        Console.WriteLine("No Data Yet!");
    }

    else if (fName.Count > 0 && cNumber.Count > 0)
    {
        for (int j = 0; j < numbers.Length; j++)
        {
            Console.WriteLine(numbers[j] + ". " + fName[j] + " - " + cNumber[j]);
        }
    }

    else if (fName.Count <= 10 && cNumber.Count <= 10)
    {
        Console.WriteLine("Maximum data acquired! Terminating program...");
    }
}

输出如下:

Output 1

Output 2

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

asp.net – 将MemoryCache与HostFileChangeMonitor init一起使用到目录获取ArgumentOutOfRangeException

asp.net – 将MemoryCache与HostFileChangeMonitor init一起使用到目录获取ArgumentOutOfRangeException

我正在使用.NET 4的System.Runtime.Caching中的MemoryCache,我希望在目录更改时使缓存条目无效.

HostFileChangeMonitor应该处理文件和目录,所以我这样添加它:

var cacheItemPolicy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(30) };
cacheItemPolicy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string> { folder }));

但后来我得到一个例外:

System.ArgumentOutOfRangeException: The UTC time represented when the offset is applied must be between year 0 and 10,000.

只有在ASP.NET网站中使用代码时才会发生这种情况.它在控制台应用程序中运行良好.

我在ms连接上找到了this,但我添加到HostFileChangeMonitor的目录存在.

谢谢你的帮助.

解决方法

回答我自己的问题:  将目录添加到HostFileChangeMonitor时似乎4.0有一个错误.

C# DirectorySearcher.FindOne();得到 thores ArgumentOutOfRangeException

C# DirectorySearcher.FindOne();得到 thores ArgumentOutOfRangeException

如何解决C# DirectorySearcher.FindOne();得到 thores ArgumentOutOfRangeException?

我正在尝试使用安全的 LDAP 查询来验证用户。我的代码在调用时抛出异常

SearchResult result = search.FindOne;

代码:

public LDAPDto LDAPLogin1(string userName,string mima)
    {
        string domainName = System.Configuration.ConfigurationManager.AppSettings["LDAPDomainName"];
        string _path = System.Configuration.ConfigurationManager.AppSettings["LDAPPath"];
        PrincipalContext ADCHECK = null;
        try
        {
            ADCHECK = new PrincipalContext(ContextType.Domain,domainName);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (!ADCHECK.ValidateCredentials(userName,mima,ContextOptions.Negotiate))
        {
            return new LDAPDto() { IsLogin = false };
        }
        string domainAndUsername = domainName + @"\" + userName;
        DirectoryEntry entry = new DirectoryEntry(_path,domainAndUsername,mima);
        DirectorySearcher search = null;
        try
        {
            search = new DirectorySearcher(entry);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        try
        {
            search.Filter = "(SAMAccountName=" + userName + ")";
        }
        catch (Exception ex)
        {
            throw ex;
        }
        search.PropertiesToLoad.AddRange(new string[] { "sn","givenname","displayName","title","department" });
        SearchResult result;
        try
        {
            result = search.FindOne();
            result.GetDirectoryEntry();
        }
        catch (Exception ex)
        {
            //there throw ex
            throw ex;
        }
        var user = new LDAPDto()
        {
            IsLogin = true,Img = null,Sn = (string)result.Properties["sn"][0],Title = (string)result.Properties["title"][0],Givenname = (string)result.Properties["givenname"][0],displayName = (string)result.Properties["displayName"][0],Department = (string)result.Properties["department"][0],};
        return user;
    }

但我不明白(我的广告是否设置了:“sn”、“givenname”、“displayName”、“title”、“department”这些值,并且它可以工作,其他广告成员只是设置了“sn” ","displayName" / "title","department" 为空,我失败了)

是否有任何重新扫描失败?

ArgumentOutOfRangeException:索引超出范围。必须是非负的并且小于集合的大小。参数名称:index.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

今天关于什么是IndexOutOfRangeException / ArgumentOutOfRangeException,如何解决?indexoutofrangeexception异常的分享就到这里,希望大家有所收获,若想了解更多关于ArgumentOutOfRangeException:索引和长度必须引用字符串中的某个位置、arraylist OutOfRangeException 上的异常、asp.net – 将MemoryCache与HostFileChangeMonitor init一起使用到目录获取ArgumentOutOfRangeException、C# DirectorySearcher.FindOne();得到 thores ArgumentOutOfRangeException等相关知识,可以在本站进行查询。

本文标签: