start_index >= lower_bound And start_index + items_to_iterate - 1 <= upper_bound
对于集合,以下公式必须为 true:
start_index >= 0 And start_index + items_to_iterate <= Count
数组或集合的起始索引永远不能是负数。
假设数组必须是从零开始的。 不是从零开始的数组可以通过该方法创建 Array.CreateInstance(Type, Int32[], Int32[]) ,并且可由 COM 互操作返回,尽管它们不符合 CLS。 以下示例演示 IndexOutOfRangeException 了尝试循环访问该方法创建的基于零的数组时引发的 Array.CreateInstance(Type, Int32[], Int32[]) 数组。
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()
open System
let values =
Array.CreateInstance(typeof<int>, [| 10 |], [| 1 |])
let mutable value = 2
// Assign values.
for i = 0 to values.Length - 1 do
values.SetValue(value, i)
value <- value * 2
// Display values.
for i = 0 to values.Length - 1 do
printf $"{values.GetValue i} "
// 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 <StartupCode$fs>.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
' Display values.
For ctr As Integer = 0 To values.Length - 1
Console.Write("{0} ", values(ctr))
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 该方法,而不是假设数组的起始索引。
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
open System
let values =
Array.CreateInstance(typeof<int>, [| 10 |], [| 1 |])
let mutable value = 2
// Assign values.
for i = values.GetLowerBound 0 to values.GetUpperBound 0 do
values.SetValue(value, i)
value <- value * 2
// Display values.
for i = values.GetLowerBound 0 to values.GetUpperBound 0 do
printf $"{values.GetValue i} "
// 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
' Display values.
For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
Console.Write("{0} ", values(ctr))
End Sub
End Module
' The example displays the following output:
' 2 4 8 16 32 64 128 256 512 1024
请注意,调用 GetLowerBound 方法以获取数组的起始索引时,还应调用 Array.GetUpperBound(Int32) 该方法来获取其结束索引。
混淆数值数组或集合中该索引处的索引和值。 在 C#) 中使用语句 (、for...inF#) 中的语句 (或For EachVisual Basic) 中的语句 (时,通常会发生foreach此问题。 以下示例演示了该问题。
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()
open System
let populateArray items maxValue =
let rnd = Random()
[| for i = 0 to items - 1 do
rnd.Next(0, maxValue + 1) |]
// Generate array of random values.
let values = populateArray 5 10
// Display each element in the array.
for value in values do
printf $"{values[value]} "
// The example displays output like the following:
// 6 4 4
// Unhandled Exception: System.IndexOutOfRangeException:
// Index was outside the bounds of the array.
// at <StartupCode$fs>.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))
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)
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()
迭代构造返回数组或集合中的每个值,而不是其索引。 若要消除异常,请使用此代码。
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
open System
let populateArray items maxValue =
let rnd = Random()
[| for i = 0 to items - 1 do
rnd.Next(0, maxValue + 1) |]
// Generate array of random values.
let values = populateArray 5 10
// Display each element in the array.
for value in values do
printf $"{value} "
// 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)
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)
Return values
End Function
End Module
' The example displays output like the following:
' 10 6 7 5 8
为属性提供无效的 DataView.Sort 列名。
违反线程安全性。 从同一 StreamReader 个对象读取、从多个线程写入同 StreamWriter 一 Hashtable 对象或从不同线程枚举对象等操作,如果无法以线程安全的方式访问对象,则可能会引发 IndexOutOfRangeException 该对象。 此异常通常是间歇性的,因为它依赖于争用条件。
如果索引值不正确或无效,或者操作数组的大小意外,则使用硬编码索引值来操作数组可能会引发异常。 若要防止操作引发 IndexOutOfRangeException 异常,可以执行以下操作:
使用 C#) 中的 foreach 语句 (循环访问数组的元素,for...in statement (in F#) , or the For Each...下一个构造 (Visual Basic) 而不是按索引循环访问元素。
通过索引循环访问元素,从方法返回 Array.GetLowerBound 的索引开始,以该方法返回 Array.GetUpperBound 的索引结尾。
如果要将一个数组中的元素分配给另一个数组,则通过比较 Array.Length 目标数组的属性,确保目标数组具有至少与源数组相同的元素。
有关实例的初始属性值的列表IndexOutOfRangeException,请参阅IndexOutOfRangeException构造函数。
以下中间语言 (IL) 指令引发 IndexOutOfRangeException:
ldelem。<type>
ldelema
斯莱姆<type>
IndexOutOfRangeException 使用具有值0x80131508的 HRESULT COR_E_INDEXOUTOFRANGE。