IndexOutOfRangeException

noun

say it: “IN-deks owt uv RAYNJ ek-SEP-shun” /ˈɪndɛks aʊt əv reɪndʒ ɪkˈsɛpʃən/

The error C# throws when code asks an array for a position it does not have.

What is IndexOutOfRangeException in C#?

IndexOutOfRangeException — The error C# throws when you ask an array for a position it doesn't have: below 0, or at its Length or above. An array of 3 items has indexes 0, 1 and 2, so index 3 is one too far.

int[] scores = [21, 14, 35];
Console.WriteLine(scores[2]); // 35
Console.WriteLine(scores[3]); // IndexOutOfRangeException

In a sentence

  • “My loop ran one time too many and threw an IndexOutOfRangeException on scores[3].”

Level: Apprentice · More C# terms at this level

Updated