TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# IndexOutOfRangeException

This C# exception article demonstrates IndexOutOfRangeException.

IndexOutOfRangeException. An IndexOutOfRangeException has occurred.

This happens in C# programs that use array types. This exception will typically occur when a statement tries to access an element at an index greater than the maximum allowable index.

Example. First this program shows the usage of the array index access. When you create an array of many elements, you can access those elements through the indices of zero through the maximum length minus one.

This means that for an array of 100 elements, you can access array[0] through array[99]. You can load and store values into these elements, which are considered variables and not values.

C# program that accesses array out of bounds

class Program
{
    static void Main()
    {
	// Allocate an array of one-hundred integers.
	// ... Then assign to positions in the array.
	// ... Assigning past the last element will throw.
	int[] array = new int[100];
	array[0] = 1;
	array[10] = 2;
	array[200] = 3;
    }
}

Exception thrown by program

Unhandled Exception: System.IndexOutOfRangeException:
Index was outside the bounds of the array.
   at Program.Main() in ...Program.cs:line 8

In Main, we use a new array of length 100. This means that the array is a reference to object data of size 100 integers. You can access the array elements through array subscripts, as with array[0], array[1], through array[99].

Tip: The top index you can access equals the total length minus one. If you access an index past 99, you get an IndexOutOfRangeException.

To avoid the exception, you can insert bounds checks, as with an if-statement. An alternative is to simply make the array length much larger when you allocate it. If you avoid if-statement checks, you can improve performance.

If

Negative array indices. You will encounter the IndexOutOfRangeException if you try to assign to an index in an array that is negative. In the C# language, native arrays are indexed beginning at zero.

Note: The first element is always available at index zero. There are ways to simulate non-zero based arrays.

Summary. Here we saw the IndexOutOfRangeException. We explained the relation between array lengths and array indices. You can always access the array indexes up to the array length minus one.

Also: This off-by-one difference is critical to keep in mind in some programs. Errors in this translation can be serious.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf