IndexOutOfRangeException IndexOutOfRangeException IndexOutOfRangeException IndexOutOfRangeException Class

定義

嘗試使用陣列或集合以外索引來存取陣列或集合項目時所擲回的例外狀況。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
繼承
IndexOutOfRangeExceptionIndexOutOfRangeExceptionIndexOutOfRangeExceptionIndexOutOfRangeException
屬性

備註

當使用不正確索引來存取陣列或集合的成員, 或是從緩衝區中的特定位置讀取或寫入時, 就會擲回例外狀況(exception)。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. 下列條件必須全部為 true;否則, IndexOutOfRangeException會擲回例外狀況:The following conditions must all be true; otherwise, an IndexOutOfRangeException exception is thrown:

    • Array.GetLowerBound對於您想要逐一查看或大於或等於0的集合, 起始索引必須大於或等於陣列的維度。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 Interop 傳回, 但不符合 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#為) 或For Each ... 來逐一查看陣列的元素。下一個結構 (在 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使用 HRESULT COR_E_INDEXOUTOFRANGE, 其值為0x80131508。IndexOutOfRangeException uses the HRESULT COR_E_INDEXOUTOFRANGE, which has the value 0x80131508.

建構函式

IndexOutOfRangeException() IndexOutOfRangeException() IndexOutOfRangeException() IndexOutOfRangeException()

初始化 IndexOutOfRangeException 類別的新執行個體。Initializes a new instance of the IndexOutOfRangeException class.

IndexOutOfRangeException(String) IndexOutOfRangeException(String) IndexOutOfRangeException(String) IndexOutOfRangeException(String)

使用指定的錯誤訊息,初始化 IndexOutOfRangeException 類別的新執行個體。Initializes a new instance of the IndexOutOfRangeException class with a specified error message.

IndexOutOfRangeException(String, Exception) IndexOutOfRangeException(String, Exception) IndexOutOfRangeException(String, Exception) 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 Data Data Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
HelpLink HelpLink HelpLink HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

取得描述目前例外狀況的訊息。Gets a message that describes the current exception.

(Inherited from Exception)
Source Source Source Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(Inherited from Exception)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(Inherited from Exception)
GetType() GetType() GetType() GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(Inherited from Exception)

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

適用於

另請參閱