1
Support Questions / Re: System.IndexOutOfRangeException: Index was outside the bounds of the array
« on: September 06, 2022, 01:21:16 AM »
This exception means that you're trying to access a collection item by index, using an invalid index. An index is invalid when it's lower than the collection's lower bound or greater than or equal to the number of elements it contains. Indexing an empty list will always throw an exception. Use a method like Add to append the item to the end of the list, or Insert to place the item in the middle of the list somewhere, etc. You cannot index into a c# list if that offset doesn't exist. IndexOutOfRangeException exception is thrown as a result of developer error. Instead of handling the exception, you should diagnose the cause of the error and correct your code.
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
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