C# | Array IndexOutofRange Exception
Last Updated :
23 Jan, 2019
C# supports the creation and manipulation of arrays, as a data structure. The index of an array is an integer value that has value in the interval [0, n-1], where n is the size of the array. If a request for a negative or an index greater than or equal to the size of the array is made, then the C# throws an
System.IndexOutOfRange Exception. This is unlike C/C++ where no index of the bound check is done. The
IndexOutOfRangeException is a Runtime Exception thrown only at runtime. The C# Compiler does not check for this error during the compilation of a program.
Example:
Csharp
// C# program to demonstrate the
// IndexOutofRange Exception in array
using System;
public class GFG {
// Main Method
public static void Main(String[] args)
{
int[] ar = {1, 2, 3, 4, 5};
// causing exception
for (int i = 0; i <= ar.Length; i++)
Console.WriteLine(ar[i]);
}
}
Runtime Error:
Unhandled Exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at GFG.Main (System.String[] args) <0x40bdbd50 + 0x00067> in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at GFG.Main (System.String[] args) <0x40bdbd50 + 0x00067> in :0
Output:
1
2
3
4
5
Here if you carefully see, the array is of size 5. Therefore while accessing its element using for loop, the maximum value of index can be 4 but in our program, it is going till 5 and thus the exception.
Let's see another example
using ArrayList:
Csharp
// C# program to demonstrate the
// IndexOutofRange Exception in array
using System;
using System.Collections;
public class GFG {
// Main Method
public static void Main(String[] args)
{
// using ArrayList
ArrayList lis = new ArrayList();
lis.Add("Geeks");
lis.Add("GFG");
Console.WriteLine(lis[2]);
}
}
Runtime Error: Here error is a bit more informative than the previous one as follows:
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.ArrayList.get_Item (Int32 index) <0x7f2d36b2ff40 + 0x00082> in :0
at GFG.Main (System.String[] args) <0x41b9fd50 + 0x0008b> in :0
[ERROR] FATAL 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.ArrayList.get_Item (Int32 index) <0x7f2d36b2ff40 + 0x00082> in :0
at GFG.Main (System.String[] args) <0x41b9fd50 + 0x0008b> in :0
Lets understand it in a bit of detail:
- Index here defines the index we are trying to access.
- The size gives us information of the size of the list.
- Since size is 2, the last index we can access is (2-1)=1, and thus the exception
The correct way to access array is :
for (int i = 0; i < ar.Length; i++)
{
}
Handling the Exception:
- Use for-each loop: This automatically handles indices while accessing the elements of an array.
- Use Try-Catch: Consider enclosing your code inside a try-catch statement and manipulate the exception accordingly. As mentioned, C# won’t let you access an invalid index and will definitely throw an IndexOutOfRangeException. However, we should be careful inside the block of the catch statement, because if we don’t handle the exception appropriately, we may conceal it and thus, create a bug in your application.
Similar Reads
4 Dimensional Array in C/C++ Prerequisite :Array in C/C++, More on array A four-dimensional (4D) array is an array of array of arrays of arrays or in other words 4D array is a array of 3D array. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays. Declarati
3 min read
4 Dimensional Array in C/C++ Prerequisite :Array in C/C++, More on array A four-dimensional (4D) array is an array of array of arrays of arrays or in other words 4D array is a array of 3D array. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays. Declarati
3 min read
Practice questions on Arrays In this article, we will discuss some important concepts related to arrays and problems based on that. Before understanding this, you should have basic idea about Arrays.Type 1. Based on array declaration - These are few key points on array declaration: A single dimensional array can be declared as
6 min read
Input an integer array without spaces in C How to input a large number (a number that cannot be stored even in long long int) without spaces? We need this large number in an integer array such that every array element stores a single digit. Input : 10000000000000000000000000000000000000000000000 We need to read it in an arr[] = {1, 0, 0....,
2 min read
Accessing array out of bounds in C/C++ Prerequisite: Arrays in C/C++In high level languages such as Java, there are functions which prevent you from accessing array out of bound by generating a exception such as java.lang.ArrayIndexOutOfBoundsException. But in case of C, there is no such functionality, so programmer need to take care of
2 min read
C# | Getting a subset of the elements from the source ArrayList ArrayList.GetRange(Int32, Int32) Method is used to get an ArrayList which will represent a subset of the elements in the source ArrayList. Syntax: public virtual System.Collections.ArrayList GetRange (int index, int count); Parameters: index: It is of Int32 type and represents the zero-based ArrayLi
3 min read