This question already has an answer here:
这个问题在这里已有答案:
- What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it? 3 answers
- 什么是IndexOutOfRangeException / ArgumentOutOfRangeException以及如何解决它? 3个答案
I'm getting one of the following errors:
我收到以下错误之一:
- "Index was out of range. Must be non-negative and less than the size of the collection"
- “指数超出范围。必须是非负数且小于集合的大小”
- "Insertion index was out of range. Must be non-negative and less than or equal to size."
- “插入指数超出范围。必须是非负数且小于或等于大小。”
- "Index was outside the bounds of the array."
- “指数数组的边界之外。”
What does it mean, and how do I fix it?
它是什么意思,我该如何解决?
See Also
IndexOutOfRangeException
ArgumentOutOfRangeException
另请参见IndexOutOfRangeException ArgumentOutOfRangeException
1 个解决方案
#1
33
Why does this error occur?
Because you tried to access an element in a collection, using a numeric index that exceeds the collection's boundaries.
因为您尝试使用超出集合边界的数字索引来访问集合中的元素。
The first element in a collection is generally located at index 0
. The last element is at index n-1
, where n
is the Size
of the collection (the number of elements it contains). If you attempt to use a negative number as an index, or a number that is larger than Size-1
, you're going to get an error.
集合中的第一个元素通常位于索引0处。最后一个元素位于索引n-1处,其中n是集合的大小(它包含的元素数)。如果您尝试使用负数作为索引,或者使用大于Size-1的数字,则会出现错误。
How indexing arrays works
When you declare an array like this:
当您声明这样的数组时:
var array = new int[6]
The first and last elements in the array are
数组中的第一个和最后一个元素是
var firstElement = array[0];
var lastElement = array[5];
So when you write:
所以当你写:
var element = array[5];
you are retrieving the sixth element in the array, not the fifth one.
您正在检索数组中的第六个元素,而不是第五个元素。
Typically, you would loop over an array like this:
通常,您将循环遍历数组,如下所示:
for (int index = 0; index < array.Length; index++)
{
Console.WriteLine(array[index]);
}
This works, because the loop starts at zero, and ends at Length-1
because index
is no longer less than Length
.
这是有效的,因为循环从零开始,并以Length-1结束,因为索引不再小于Length。
This, however, will throw an exception:
但是,这将抛出异常:
for (int index = 0; index <= array.Length; index++)
{
Console.WriteLine(array[index]);
}
Notice the <=
there? index
will now be out of range in the last loop iteration, because the loop thinks that Length
is a valid index, but it is not.
注意<=那里? index现在将在最后一个循环迭代中超出范围,因为循环认为Length是一个有效索引,但事实并非如此。
How other collections work
Lists work the same way, except that you generally use Count
instead of Length
. They still start at zero, and end at Count - 1
.
列表以相同的方式工作,除了您通常使用Count而不是Length。它们仍然从0开始,以Count-1结束。
for (int index = 0; i < list.Count; index++)
{
Console.WriteLine(list[index]);
}
However, you can also iterate through a list using foreach
, avoiding the whole problem of indexing entirely:
但是,您也可以使用foreach迭代列表,完全避免索引的整个问题:
foreach (var element in list)
{
Console.WriteLine(element.ToString());
}
You cannot index an element that hasn't been added to a collection yet.
您无法索引尚未添加到集合中的元素。
var list = new List<string>();
list.Add("Zero");
list.Add("One");
list.Add("Two");
Console.WriteLine(list[3]); // Throws exception.
#1
33
Why does this error occur?
Because you tried to access an element in a collection, using a numeric index that exceeds the collection's boundaries.
因为您尝试使用超出集合边界的数字索引来访问集合中的元素。
The first element in a collection is generally located at index 0
. The last element is at index n-1
, where n
is the Size
of the collection (the number of elements it contains). If you attempt to use a negative number as an index, or a number that is larger than Size-1
, you're going to get an error.
集合中的第一个元素通常位于索引0处。最后一个元素位于索引n-1处,其中n是集合的大小(它包含的元素数)。如果您尝试使用负数作为索引,或者使用大于Size-1的数字,则会出现错误。
How indexing arrays works
When you declare an array like this:
当您声明这样的数组时:
var array = new int[6]
The first and last elements in the array are
数组中的第一个和最后一个元素是
var firstElement = array[0];
var lastElement = array[5];
So when you write:
所以当你写:
var element = array[5];
you are retrieving the sixth element in the array, not the fifth one.
您正在检索数组中的第六个元素,而不是第五个元素。
Typically, you would loop over an array like this:
通常,您将循环遍历数组,如下所示:
for (int index = 0; index < array.Length; index++)
{
Console.WriteLine(array[index]);
}
This works, because the loop starts at zero, and ends at Length-1
because index
is no longer less than Length
.
这是有效的,因为循环从零开始,并以Length-1结束,因为索引不再小于Length。
This, however, will throw an exception:
但是,这将抛出异常:
for (int index = 0; index <= array.Length; index++)
{
Console.WriteLine(array[index]);
}
Notice the <=
there? index
will now be out of range in the last loop iteration, because the loop thinks that Length
is a valid index, but it is not.
注意<=那里? index现在将在最后一个循环迭代中超出范围,因为循环认为Length是一个有效索引,但事实并非如此。
How other collections work
Lists work the same way, except that you generally use Count
instead of Length
. They still start at zero, and end at Count - 1
.
列表以相同的方式工作,除了您通常使用Count而不是Length。它们仍然从0开始,以Count-1结束。
for (int index = 0; i < list.Count; index++)
{
Console.WriteLine(list[index]);
}
However, you can also iterate through a list using foreach
, avoiding the whole problem of indexing entirely:
但是,您也可以使用foreach迭代列表,完全避免索引的整个问题:
foreach (var element in list)
{
Console.WriteLine(element.ToString());
}
You cannot index an element that hasn't been added to a collection yet.
您无法索引尚未添加到集合中的元素。
var list = new List<string>();
list.Add("Zero");
list.Add("One");
list.Add("Two");
Console.WriteLine(list[3]); // Throws exception.