From the course: Debugging in C#

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

IndexOutOfRangeException

IndexOutOfRangeException

From the course: Debugging in C#

Start my 1-month free trial

IndexOutOfRangeException

- [Instructor] Another common error in C# as well as other programming languages is the Index Out of Range Error or the Index Out of Range Exception. This error occurs when the program tries to access an element in a collection using an index that exceeds the collection's boundaries. Typically, the first element in a collection is located at index zero and the last element is at index n minus one, where n is the length or size of the collection. If you attempt to access an element outside of these boundaries, either with a negative index or an index that is larger than the length minus one, you will get an error because you are accessing something that doesn't exist inside of the collection. Let's take a look at an example of this in code. In this program, we have an array called numbers and we put five items into it. Then we print out the item at index zero, index negative one, and index five. Let's run the program and see what happens. And so the item at index zero is going to be 10…

Contents