IndexOutOfRangeException 类

定义

试图访问索引超出界限的数组或集合的元素时引发的异常。The exception that is thrown when an attempt is made to access an element of an array or collection with an index that is outside its bounds.

public ref class IndexOutOfRangeException sealed : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class IndexOutOfRangeException : SystemException
type IndexOutOfRangeException = class
    inherit SystemException
Public NotInheritable Class IndexOutOfRangeException
Inherits SystemException
继承
IndexOutOfRangeException
属性

注解

当使用无效索引访问数组或集合的成员, 或者从缓冲区中的特定位置读取或写入时, 将引发异常。IndexOutOfRangeExceptionAn IndexOutOfRangeException exception is thrown when an invalid index is used to access a member of an array or a collection, or to read or write from a particular location in a buffer. 此异常继承自类Exception , 但不添加唯一成员。This exception inherits from the Exception class but adds no unique members.

通常, 由于IndexOutOfRangeException开发人员错误, 将引发异常。Typically, an IndexOutOfRangeException exception is thrown as a result of developer error. 应该诊断错误的原因并更正代码, 而不是处理异常。Instead of handling the exception, you should diagnose the cause of the error and correct your code. 此错误的最常见原因是:The most common causes of the error are:

  • 忘记集合或从零开始的数组的上限比其成员或元素的数量少一, 如下面的示例所示。Forgetting that the upper bound of a collection or a zero-based array is one less than its number of members or elements, as the following example illustrates.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          List<Char> characters = new List<Char>();
          characters.InsertRange(0, new Char[] { 'a', 'b', 'c', 'd', 'e', 'f' } );
          for (int ctr = 0; ctr <= characters.Count; ctr++)
             Console.Write("'{0}'    ", characters[ctr]);
       }
    }
    // The example displays the following output:
    //    'a'    'b'    'c'    'd'    'e'    'f'
    //    Unhandled Exception: 
    //    System.ArgumentOutOfRangeException: 
    //    Index was out of range. Must be non-negative and less than the size of the collection.
    //    Parameter name: index
    //       at Example.Main()
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim characters As New List(Of Char)()
          characters.InsertRange(0, { "a"c, "b"c, "c"c, "d"c, "e"c, "f"c} )
          For ctr As Integer = 0 To characters.Count
             Console.Write("'{0}'    ", characters(ctr))
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '    'a'    'b'    'c'    'd'    'e'    'f'
    '    Unhandled Exception: 
    '    System.ArgumentOutOfRangeException: 
    '    Index was out of range. Must be non-negative and less than the size of the collection.
    '    Parameter name: index
    '       at System.Collections.Generic.List`1.get_Item(Int32 index)
    '       at Example.Main()
    

    若要更正此错误, 可以使用如下所示的代码。To correct the error, you can use code like the following.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          List<Char> characters = new List<Char>();
          characters.InsertRange(0, new Char[] { 'a', 'b', 'c', 'd', 'e', 'f' } );
          for (int ctr = 0; ctr < characters.Count; ctr++)
             Console.Write("'{0}'    ", characters[ctr]);
       }
    }
    // The example displays the following output:
    //        'a'    'b'    'c'    'd'    'e'    'f'
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim characters As New List(Of Char)()
          characters.InsertRange(0, { "a"c, "b"c, "c"c, "d"c, "e"c, "f"c} )
          For ctr As Integer = 0 To characters.Count - 1
             Console.Write("'{0}'    ", characters(ctr))
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '       'a'    'b'    'c'    'd'    'e'    'f'
    

    或者, 您可以使用foreach语句 (在中C#) 或For Each语句 (在 Visual Basic 中), 而不是按索引来循环访问数组中的所有元素。Alternately, instead of iterating all the elements in the array by their index, you can use the foreach statement (in C#) or the For Each statement (in Visual Basic).

  • 尝试将一个数组元素分配给另一个数组, 该数组的数组与原始数组相比, 其元素少。Attempting to assign an array element to another array that has not been adequately dimensioned and that has fewer elements than the original array. 下面的示例尝试将value1数组中的最后一个元素分配给value2数组中的同一元素。The following example attempts to assign the last element in the value1 array to the same element in the value2 array. 但是, value2数组已错误地标注为包含六个或七个元素。However, the value2 array has been incorrectly dimensioned to have six instead of seven elements. 因此, 分配会引发IndexOutOfRangeException异常。As a result, the assignment throws an IndexOutOfRangeException exception.

    public class Example
    {
       public static void Main()
       {
          int[] values1 = { 3, 6, 9, 12, 15, 18, 21 };
          int[] values2 = new int[6];
          
          // Assign last element of the array to the new array.
          values2[values1.Length - 1] = values1[values1.Length - 1];
       }
    }
    // The example displays the following output:
    //       Unhandled Exception: 
    //       System.IndexOutOfRangeException: 
    //       Index was outside the bounds of the array.
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
          Dim values1() As Integer = { 3, 6, 9, 12, 15, 18, 21 }
          Dim values2(5) As Integer
          
          ' Assign last element of the array to the new array.
          values2(values1.Length - 1) = values1(values1.Length - 1)
       End Sub
    End Module
    ' The example displays the following output:
    '       Unhandled Exception: 
    '       System.IndexOutOfRangeException: 
    '       Index was outside the bounds of the array.
    '       at Example.Main()
    
  • 使用搜索方法返回的值从特定索引位置开始循环访问数组或集合的一部分。Using a value returned by a search method to iterate a portion of an array or collection starting at a particular index position. 如果忘记检查搜索操作是否找到了匹配项, 则运行时将引发IndexOutOfRangeException异常, 如本示例中所示。If you forget to check whether the search operation found a match, the runtime throws an IndexOutOfRangeException exception, as shown in this example.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       static List<int> numbers = new List<int>();
    
       public static void Main()
       {
          int startValue; 
          string[] args = Environment.GetCommandLineArgs();
          if (args.Length < 2) 
             startValue = 2;
          else 
             if (! Int32.TryParse(args[1], out startValue))
                startValue = 2;
    
          ShowValues(startValue);
       }
       
       private static void ShowValues(int startValue)
       {   
          // Create a collection with numeric values.
          if (numbers.Count == 0)  
             numbers.AddRange( new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} );
    
          // Get the index of a startValue.
          Console.WriteLine("Displaying values greater than or equal to {0}:",
                            startValue);
          int startIndex = numbers.IndexOf(startValue);
          // Display all numbers from startIndex on.
          for (int ctr = startIndex; ctr < numbers.Count; ctr++)
             Console.Write("    {0}", numbers[ctr]);
       }
    }
    // The example displays the following output if the user supplies
    // 7 as a command-line parameter:
    //    Displaying values greater than or equal to 7:
    //    
    //    Unhandled Exception: System.ArgumentOutOfRangeException: 
    //    Index was out of range. Must be non-negative and less than the size of the collection.
    //    Parameter name: index
    //       at System.Collections.Generic.List`1.get_Item(Int32 index)
    //       at Example.ShowValues(Int32 startValue)
    //       at Example.Main()
    
    Imports System.Collections.Generic
    
    Module Example
       Dim numbers As New List(Of Integer)
    
       Public Sub Main()
          Dim startValue As Integer 
          Dim args() As String = Environment.GetCommandLineArgs()
          If args.Length < 2 Then
             startValue = 2
          Else
             If Not Int32.TryParse(args(1), startValue) Then
                startValue = 2
             End If   
          End If
          ShowValues(startValue)
       End Sub
       
       Private Sub ShowValues(startValue As Integer)   
          ' Create a collection with numeric values.
          If numbers.Count = 0 Then 
             numbers.AddRange( { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} )
          End If   
          ' Get the index of a particular number, in this case 7.
          Console.WriteLine("Displaying values greater than or equal to {0}:",
                            startValue)
          Dim startIndex As Integer = numbers.IndexOf(startValue)
          ' Display all numbers from startIndex on.
          For ctr As Integer = startIndex To numbers.Count - 1
             Console.Write("    {0}", numbers(ctr))
          Next
       End Sub
    End Module
    ' The example displays the following output if the user supplies
    ' 7 as a command-line parameter:
    '    Displaying values greater than or equal to 7:
    '    
    '    Unhandled Exception: System.ArgumentOutOfRangeException: 
    '    Index was out of range. Must be non-negative and less than the size of the collection.
    '    Parameter name: index
    '       at System.Collections.Generic.List`1.get_Item(Int32 index)
    '       at Example.ShowValues(Int32 startValue)
    '       at Example.Main()
    

    在这种情况下List<T>.IndexOf , 如果找不到匹配项, 该方法将返回-1, 这是无效的索引值。In this case, the List<T>.IndexOf method returns -1, which is an invalid index value, when it fails to find a match. 若要更正此错误, 请检查搜索方法的返回值, 然后循环访问数组, 如本示例中所示。To correct this error, check the search method's return value before iterating the array, as shown in this example.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       static List<int> numbers = new List<int>();
    
       public static void Main()
       {
          int startValue; 
          string[] args = Environment.GetCommandLineArgs();
          if (args.Length < 2) 
             startValue = 2;
          else 
             if (! Int32.TryParse(args[1], out startValue))
                startValue = 2;
    
          ShowValues(startValue);
       }
       
       private static void ShowValues(int startValue)
       {   
          // Create a collection with numeric values.
          if (numbers.Count == 0)  
             numbers.AddRange( new int[] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} );
    
          // Get the index of startValue.
          int startIndex = numbers.IndexOf(startValue);
          if (startIndex < 0) {
             Console.WriteLine("Unable to find {0} in the collection.", startValue);
          }
          else {
             // Display all numbers from startIndex on.
             Console.WriteLine("Displaying values greater than or equal to {0}:",
                            startValue);
             for (int ctr = startIndex; ctr < numbers.Count; ctr++)
                Console.Write("    {0}", numbers[ctr]);
          }
       }
    }
    // The example displays the following output if the user supplies
    // 7 as a command-line parameter:
    //      Unable to find 7 in the collection.
    
    Imports System.Collections.Generic
    
    Module Example
       Dim numbers As New List(Of Integer)
    
       Public Sub Main()
          Dim startValue As Integer 
          Dim args() As String = Environment.GetCommandLineArgs()
          If args.Length < 2 Then
             startValue = 2
          Else
             If Not Int32.TryParse(args(1), startValue) Then
                startValue = 2
             End If   
          End If
          ShowValues(startValue)
       End Sub
       
       Private Sub ShowValues(startValue As Integer)   
          ' Create a collection with numeric values.
          If numbers.Count = 0 Then 
             numbers.AddRange( { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22} )
          End If   
          ' Get the index of startValue.
          Dim startIndex As Integer = numbers.IndexOf(startValue)
          If startIndex < 0 Then
             Console.WriteLine("Unable to find {0} in the collection.", startValue)
          Else
             ' Display all numbers from startIndex on.
             Console.WriteLine("Displaying values greater than or equal to {0}:",
                            startValue)
             For ctr As Integer = startIndex To numbers.Count - 1
                Console.Write("    {0}", numbers(ctr))
             Next
          End If
       End Sub
    End Module
    ' The example displays the following output if the user supplies
    '       Unable to find 7 in the collection.
    
  • 尝试使用或枚举查询返回的结果集、集合或数组, 而不测试返回的对象是否具有任何有效数据。Trying to use or enumerate a result set, collection, or array returned by a query without testing whether the returned object has any valid data.

  • 使用计算得出的值定义起始索引、结束索引或要迭代的项数。Using a computed value to define the starting index, the ending index, or the number of items to be iterated. 如果计算结果不是预期的, 则可能会导致IndexOutOfRangeException异常。If the result of the computation is unexpected, it might result in an IndexOutOfRangeException exception. 应在计算索引值时检查程序的逻辑, 并在循环访问数组或集合之前验证该值。You should check your program's logic in calculating the index value and validate the value before iterating the array or collection. 必须满足以下条件:否则, IndexOutOfRangeException将引发异常:The following conditions must all be true; otherwise, an IndexOutOfRangeException exception is thrown:

    • Array.GetLowerBound对于要循环访问的数组的维度, 或大于或等于集合的集合, 开始索引必须大于或等于。The starting index must be greater than or equal to Array.GetLowerBound for the dimension of the array that you want to iterate, or greater than or equal to 0 for a collection.

    • 对于要循环访问的Array.GetUpperBound数组的维度, 或不能大于或等于集合的Count属性, 结束索引不能超过。The ending index cannot exceed Array.GetUpperBound for the dimension of the array that you want to iterate, or cannot be greater than or equal to the Count property of a collection.

    • 对于要循环访问的数组的维度, 以下公式必须为 true:The following equation must be true for the dimension of the array that you want to iterate:

      start_index >= lower_bound And start_index + items_to_iterate - 1 <= upper_bound  
      

      对于集合, 以下公式必须为 true:For a collection, the following equation must be true:

      start_index >= 0 And start_index + items_to_iterate <= Count  
      

      提示

      数组或集合的起始索引绝不能为负数。The starting index of an array or collection can never be a negative number.

  • 假设数组必须从零开始。Assuming that an array must be zero-based. 不是从零开始的数组可以通过Array.CreateInstance(Type, Int32[], Int32[])方法创建, 并且可由 COM 互操作返回, 尽管它们不符合 CLS。Arrays that are not zero-based can be created by the Array.CreateInstance(Type, Int32[], Int32[]) method and can be returned by COM interop, although they aren't CLS-compliant. 下面的示例演示IndexOutOfRangeException当你尝试循环访问Array.CreateInstance(Type, Int32[], Int32[])由方法创建的非从零开始的数组时引发的。The following example illustrates the IndexOutOfRangeException that is thrown when you try to iterate a non-zero-based array created by the Array.CreateInstance(Type, Int32[], Int32[]) method.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Array values = Array.CreateInstance(typeof(int), new int[] { 10 }, 
                                              new int[] { 1 });
          int value = 2;
          // Assign values.
          for (int ctr = 0; ctr < values.Length; ctr++) {
             values.SetValue(value, ctr);
             value *= 2;
          }
          
          // Display values.
          for (int ctr = 0; ctr < values.Length; ctr++)
             Console.Write("{0}    ", values.GetValue(ctr));
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: 
    //    System.IndexOutOfRangeException: Index was outside the bounds of the array.
    //       at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices)
    //       at System.Array.SetValue(Object value, Int32 index)
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
          Dim values = Array.CreateInstance(GetType(Integer), { 10 }, { 1 })
          Dim value As Integer = 2
          ' Assign values.
          For ctr As Integer = 0 To values.Length - 1
             values(ctr) = value
             value *= 2
          Next
          
          ' Display values.
          For ctr As Integer = 0 To values.Length - 1
             Console.Write("{0}    ", values(ctr))
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: 
    '    System.IndexOutOfRangeException: Index was outside the bounds of the array.
    '       at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices)
    '       at System.Array.SetValue(Object value, Int32 index)
    '       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateIndexSetComplex(Obje
    '    ct Instance, Object[] Arguments, String[] ArgumentNames, Boolean OptimisticSet, Boolean RV
    '    alueBase)
    '       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateIndexSet(Object Instance,
    '    Object[] Arguments, String[] ArgumentNames)
    '       at Example.Main()
    

    若要更正此错误, 如下面的示例所示, 可以调用GetLowerBound方法, 而不是假设数组的起始索引。To correct the error, as the following example does, you can call the GetLowerBound method instead of making assumptions about the starting index of an array.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Array values = Array.CreateInstance(typeof(int), new int[] { 10 }, 
                                              new int[] { 1 });
          int value = 2;
          // Assign values.
          for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++) {
             values.SetValue(value, ctr);
             value *= 2;
          }
          
          // Display values.
          for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
             Console.Write("{0}    ", values.GetValue(ctr));
       }
    }
    // The example displays the following output:
    //        2    4    8    16    32    64    128    256    512    1024
    
    Module Example
       Public Sub Main()
          Dim values = Array.CreateInstance(GetType(Integer), { 10 }, { 1 })
          Dim value As Integer = 2
          ' Assign values.
          For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
             values(ctr) = value
             value *= 2
          Next
          
          ' Display values.
          For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
             Console.Write("{0}    ", values(ctr))
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '       2    4    8    16    32    64    128    256    512    1024
    

    请注意, 当调用GetLowerBound方法来获取数组的起始索引时, 还应Array.GetUpperBound(Int32)调用方法来获取其结束索引。Note that when you call the GetLowerBound method to get the starting index of an array, you should also call the Array.GetUpperBound(Int32) method to get its ending index.

  • 将索引与数字数组或集合中该索引处的值混淆。Confusing an index and the value at that index in a numeric array or collection. 当使用语句 (在中foreach C#) 或For Each语句 (在 Visual Basic 中) 时, 通常会发生此问题。This issue usually occurs when using the foreach statement (in C#) or the For Each statement (in Visual Basic). 以下示例演示了该问题。The following example illustrates the problem.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          // Generate array of random values.
          int[] values = PopulateArray(5, 10);
          // Display each element in the array.
          foreach (var value in values)
             Console.Write("{0}   ", values[value]);
       }
    
       private static int[] PopulateArray(int items, int maxValue)
       {
          int[] values = new int[items];
          Random rnd = new Random();
          for (int ctr = 0; ctr < items; ctr++)
             values[ctr] = rnd.Next(0, maxValue + 1);   
    
          return values;                                                      
       }
    }
    // The example displays output like the following:
    //    6   4   4
    //    Unhandled Exception: System.IndexOutOfRangeException: 
    //    Index was outside the bounds of the array.
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
          ' Generate array of random values.
          Dim values() As Integer = PopulateArray(5, 10)
          ' Display each element in the array.
          For Each value In values
             Console.Write("{0}   ", values(value))
          Next
       End Sub
       
       Private Function PopulateArray(items As Integer, 
                                      maxValue As Integer) As Integer()
          Dim values(items - 1) As Integer
          Dim rnd As New Random()
          For ctr As Integer = 0 To items - 1
             values(ctr) = rnd.Next(0, maxValue + 1)   
          Next    
          Return values                                                      
       End Function
    End Module
    ' The example displays output like the following:
    '    6   4   4
    '    Unhandled Exception: System.IndexOutOfRangeException: 
    '    Index was outside the bounds of the array.
    '       at Example.Main()
    

    迭代构造返回数组或集合中的每个值, 而不是其索引。The iteration construct returns each value in an array or collection, not its index. 若要消除此异常, 请使用此代码。To eliminate the exception, use this code.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          // Generate array of random values.
          int[] values = PopulateArray(5, 10);
          // Display each element in the array.
          foreach (var value in values)
             Console.Write("{0}   ", value);
       }
    
       private static int[] PopulateArray(int items, int maxValue)
       {
          int[] values = new int[items];
          Random rnd = new Random();
          for (int ctr = 0; ctr < items; ctr++)
             values[ctr] = rnd.Next(0, maxValue + 1);   
    
          return values;                                                      
       }
    }
    // The example displays output like the following:
    //        10   6   7   5   8
    
    Module Example
       Public Sub Main()
          ' Generate array of random values.
          Dim values() As Integer = PopulateArray(5, 10)
          ' Display each element in the array.
          For Each value In values
             Console.Write("{0}   ", value)
          Next
       End Sub
       
       Private Function PopulateArray(items As Integer, 
                                      maxValue As Integer) As Integer()
          Dim values(items - 1) As Integer
          Dim rnd As New Random()
          For ctr As Integer = 0 To items - 1
             values(ctr) = rnd.Next(0, maxValue + 1)   
          Next    
          Return values                                                      
       End Function
    End Module
    ' The example displays output like the following:
    '       10   6   7   5   8
    
  • DataView.Sort属性提供了无效的列名称。Providing an invalid column name to the DataView.Sort property.

  • 违反线程安全。Violating thread safety. 如果对象不是在中访问StreamReader的, 则从同一对象StreamWriter读取、从多个线程写入同一对象或Hashtable从不同线程中枚举对象的操作可能会引发。 IndexOutOfRangeException线程安全的方式。Operations such as reading from the same StreamReader object, writing to the same StreamWriter object from multiple threads, or enumerating the objects in a Hashtable from different threads can throw an IndexOutOfRangeException if the object isn't accessed in a thread-safe way. 此异常通常是间歇性的, 因为它依赖于争用条件。This exception is typically intermittent because it relies on a race condition.

如果索引值不正确或无效, 或者如果正在操作的数组的大小不正确, 则使用硬编码索引值来操作数组可能会引发异常。Using hard-coded index values to manipulate an array is likely to throw an exception if the index value is incorrect or invalid, or if the size of the array being manipulation is unexpected. 若要防止操作引发IndexOutOfRangeException异常, 可以执行以下操作:To prevent an operation from throwing an IndexOutOfRangeException exception, you can do the following:

  • 使用foreach语句 (在中C#为) 或为每个 .。。下一构造 (在 Visual Basic 中), 而不是按索引来循环访问元素。Iterate the elements of the array using the foreach statement (in C#) or the For Each...Next construct (in Visual Basic) instead of iterating elements by index.

  • 按索引从Array.GetLowerBound方法返回的索引开始循环访问元素, 并以Array.GetUpperBound方法返回的索引结束。Iterate the elements by index starting with the index returned by the Array.GetLowerBound method and ending with the index returned by the Array.GetUpperBound method.

  • 如果要将一个数组中的元素分配给另一个数组, 请确保目标数组的元素与源数组的元素数至少与源Array.Length数组的数目相同。If you are assigning elements in one array to another, ensure that the target array has at least as many elements as the source array by comparing their Array.Length properties.

有关实例的初始属性值的列表IndexOutOfRangeException,请参阅IndexOutOfRangeException构造函数。For a list of initial property values for an instance of IndexOutOfRangeException, see the IndexOutOfRangeException constructors.

以下中间语言 (IL) 指令引发IndexOutOfRangeException:The following intermediate language (IL) instructions throw IndexOutOfRangeException:

  • ldelem.<类型 >ldelem.<type>

  • ldelemaldelema

  • stelem.<type>stelem.<type>

IndexOutOfRangeException使用 COR_E_INDEXOUTOFRANGE 值为0x80131508 的 HRESULT。IndexOutOfRangeException uses the HRESULT COR_E_INDEXOUTOFRANGE, which has the value 0x80131508.

构造函数

IndexOutOfRangeException()

初始化 IndexOutOfRangeException 类的新实例。Initializes a new instance of the IndexOutOfRangeException class.

IndexOutOfRangeException(String)

用指定的错误消息初始化 IndexOutOfRangeException 类的新实例。Initializes a new instance of the IndexOutOfRangeException class with a specified error message.

IndexOutOfRangeException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 IndexOutOfRangeException 类的新实例。Initializes a new instance of the IndexOutOfRangeException class with a specified error message and a reference to the inner exception that is the cause of this exception.

属性

Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(继承自 Exception)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(继承自 Exception)
Message

获取描述当前异常的消息。Gets a message that describes the current exception.

(继承自 Exception)
Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(继承自 Exception)
TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(继承自 Exception)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(继承自 Exception)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(继承自 Exception)
GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。Creates and returns a string representation of the current exception.

(继承自 Exception)

事件

SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(继承自 Exception)

适用于

另请参阅