C# | Array IndexOutofRange Exception Last Updated : 23 Jan, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: for(int variable_name in array_variable) { // loop body } 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. Comment More infoAdvertise with us Next Article C# | Array IndexOutofRange Exception A Akanksha_Rai Follow Improve Article Tags : Misc C# CSharp-Exception Practice Tags : Misc Similar Reads Array.LastIndexOf Method in C# | Set - 1 Array.LastIndexOf method is used to find the last matching element in a one-dimensional array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows: LastIndexO 8 min read array at() function in C++ STL The array::at() is a built-in function in C++ STL which returns a reference to the element present at location i in given array. Syntax: array_name.at(i) Parameters: The function accepts a single mandatory parameter i which specifies the location. Return value: The function returns an element presen 2 min read Exception Handling in C# An exception is defined as an event that occurs during the execution of a program that is unexpected by the program code. The actions to be performed in case of occurrence of an exception is not known to the program. In such a case, we create an exception object and call the exception handler code. 6 min read C# | Finding the index of last element in the array GetUpperBound() Method is used to find the index of the last element of the specified dimension in the array. Syntax: public int GetUpperBound (int dimension); Here, dimension is a zero-based dimension of the array whose upper bound needs to be determined. Return Value:The return type of this method 2 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 C# | Finding the index of first element in the array GetLowerBound() Method is used to find the index of the first element of the specified dimension in the array. Syntax: public int GetLowerBound (int dimension); Here, dimension is a zero-based dimension of the array whose lower bound needs to be determined. Return Value: The return type of this meth 2 min read C# | Array.ConstrainedCopy() Method This method is used to copy a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely. Syntax: public static void ConstrainedCop 9 min read Array getInt() Method in Java The java.lang.reflect.Array.getInt() is an inbuilt method in Java and is used to return an element at the given index from the specified Array as a int. Syntax Array.getInt(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The object array whose index is to 3 min read Array getLong() Method in Java The java.lang.reflect.Array.getLong() is an inbuilt method in Java and is used to return an element at the given index from a specified Array as a long. Syntax: Array.getLong(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is 3 min read C# | Total number of elements present in an array Array.GetLength(Int32) Method is used to find the total number of elements present in the specified dimension of the Array. Syntax: public int GetLength (int dimension); Here, dimension is a zero-based dimension of the Array whose length needs to be determined.Return value: The return type of this m 2 min read Like