Understanding IndexOutOfRangeException and ArgumentOutOfRangeException


You’ve received the following error: Index was outside the bounds of the array with an IndexOutOfRangeException or you received:

An exception of type ‘System.ArgumentOutOfRangeException’ occurred in mscorlib.dll but was not handled in user code Additional information: Index was out of range. Must be non-negative and less than the size of the collection with an ArgumentOutOfRangeException.

So what gives? Here is what you need to know.

What is IndexOutOfRangeException error message: Index was outside the bounds of the array

This exception occurs when you try to access a collection item by using an invalid index. An index that is invalid happens when it is lower than the collection’s lower bound or greater than or equal to the number of elements it contains.

For example -1 or if there were 4 elements, using the number 4 because collections start at a 0; commonly called 0-based indexing.

What is An exception of type System.ArgumentOutOfRangeException

An ArgumentOutOfRangeException is very similar to IndexOutOfRangeException exception where you are trying to access an index that is not within the bounds of the list.

Typically the ArgumentOutOfRangeException is thrown when using List whereas IndexOutOfRangeException more commonly occurs with arrays.

How to solve IndexOutOfRangeException and ArgumentOutOfRangeException

The most common way these exceptions are thrown when you are using search functions like IndexOf or FindIndex.

When you are using these functions, if the value is not returned then the result returns -1 which is not inside the lower bounds of the array.

After performing a function call like this adding a simple check like: if (index > -1) or if (index >= 0) proceed.

You can also check that a value is not trying to access outside of the array’s upper bounds by doing something like: if (index < array.Length) proceed.

Type: System.ArgumentOutOfRangeException

public class ArgumentOutOfRangeException : ArgumentException.

ArgumentOutOverRangeException is extensively used: ArgumentOutofRangeException behaves the same as ArgumentException. The system provides an interface to distinguish the exceptions caused from valid arguments not null and exceptions from non existent arguments.

Exceptions caused by arguments that are invalid can be seen under ArgumentException. ArgumentOutOutofrangeException is using HRESULT COR_E_ARGUMENTOUTOFRANGE, which has value of 0x801315002. See arguments OutOfRangeException – ArgumentOutOfRange exception. #ctor constructor.


The exception occurs when a program runs at an incorrect rate at runtime that violates a system or application restriction. Possible reasons for an exception include attempting to connect to a database that no longer exists when programs try to multiply numbers with zero.

In cases where such errors happen, the system detects them and makes an exception. Catching exceptions helps to resolve unexpected failures by defining the code block to be executed when an exception throws. Several common exception types are helpful.

When It Is Thrown

When you give a declared array that is 0 to 3 values outside the range causes indexExceptions to be thrown. Keep this in mind when storing arrays. Arrays lengths. C# is commonly used to build arrays.

Besides it indicates that if you have a polygon array it is impossible for the first element to have the ‘index zero’ as well that last element is the index ‘long’ 1 which consists of all the elements in a table so it isn’t working.

Length of the two dimensions are required using array. GettingLength(): Upper Bound is not exclusive. In the below examples I created a simple bidimensional array for color.

How to Avoid

For the purposes here, I will assume arrays are mono-differenced based on 0-value. In case you want strictness in the code (or are building a library), you could substitute 0.

Longer than GetUPPERBOUND(0) in a given context (of course, when your system has parameter types. Array it’s not applicable for this t[]). Please note that if the upper bound is included then it is rewritten as follows:

Please note that this is not allowed (it throws InvalidCastException) and if your parameters are T you are protected against this error.

Encountering the ArgumentOutOfRangeExceptionException Type

ArgumentOutOfRangException is thrown out if the method passed to the method has not been null and has a value which cannot be found within the specified value sets.

This exception type uses ParamNames and ActualValues that aid in understanding the cause of this exception type.

The ParamName property indicates parameter names for invalid arguments while the ActualValue property indicates an invalid value when a value is found.

How to Debug?

My view is that most of these issues on SO are easily eliminated. The time spent writing the answer will easily far outweigh the time spent debugging the problem.

I will not repeat the words I said. You have code, there are exception messages with stack trace. Find your error, check how indexes increase.

Is that correct? Check if array’s allocations reflect how Indexed Indexes grow?

What is IndexOutOfRangeException: Index was outside the bounds of the array

This exception occurs if a collection item is accessible through a valid indexed item. A null index can be created with scalability if it is greater than its lowest bound or greater than its number in a collection.

The numbers 4 and 1 are common if the collection begins in 0.

ArgumentOutOfRangeException class

An exception that occurs when an argument’s value exceeds its allowed value range in the invoked method.

The exception means that your query is accessed from indexed items with no valid indexes. Indexes are invalid if they are below the lowest limit of the collection or greater than the number contained in the collection.

Frequently Asked Questions

What is System ArgumentOutOfRangeException in C#?

Commentary. ArgumentOut ofRangeExceptions are thrown unless a method invoked is invoked in which at least a method argument is not null and contains a non null value.

How do you solve ArgumentOutOfRangeException?

Arrays are zero-baseindexed. They are therefore indexed from zero to the number of element – 1. Moreover, in your code there shouldn’t be a hardcode to calculate the size of an array. Ideally it would eliminate the exceptions on the array indices.

What is System IndexOutOfRangeException?

An IndexOutofRangeException is an exception which is thrown while accessing an element in an archive whose index does not exist. It occurs when invalid indexed records can be found in a collection.

What is ArgumentNullException in C#?

ArgumentNullException Exceptions are created when methods are invoked with at least one argument null.

How do you throw an ArgumentOutOfRangeException?

ArgumentOutofRangeExceptions will erupt when method methods are invoked.

How do you handle IndexOutOfRangeException?

Exceptions are compiled into the try block. A catch phrase is inserted shortly following it to handle indexOutOfRangeException when it does occur.

This catch block handles an Index Out Of RangeException and attempts a different argument Out Of RangeException instead.

How do you handle an argument out of range exception in C#?

The OutofRangeException exception occurs if the argument passed to a method does not have any null value and does not contain expected values.

Its exception types use ParamName and ActualValue properties to understand why an exception occurred.

What does argument out of range mean?

Typically an error indicating an argument outside range when viewed is indicative of a computer error. MMDDYYYYYYYYYYYYYYYYYYYYYY.

Number and currency have been established to US standards.

How do you handle an array out of bound exception in C#?

Use every loop: It automatically handles indices as a user is able to view arrays.

Use try catch: Consider including your code in the try-catch statement to control the exceptions accordingly. As stated above, C# will not allow for access to invalid indexes.

What is argument out of range exception?

The argumentsOutOfRangeException exception is thrown if a method is invoked and the arguments passed are null and contain invalid arguments.

How do I resolve IndexOutOfRangeException?

The error can then be corrected by checking a returned array before iterating the array as shown in a previous example. Use systems. Collections. Generic, public class Examples: static list numbers = New List().

What is argument exception in C#?

ArgumentException will throw when the method is invoked unless it is not specified in its parametric specification. This property is used for identifying invalid arguments.

What is IndexOutOfRangeException in C#?

An IndexOutOfrangeExceptionException is thrown if an invalid index is accessed in a buffer. This exception inherits from Exception classes and does not include a specific member.

How do you check if an array is out of bounds C#?

Another way to verify the array’s limits is to create a function. It will test whether the index has a “limit”. The result is false when the index has less than 0.

These functions must be built into an array.

How do I raise an exception in C#?

Exception throws will either be executed in C# or in Visual Basic throw. It’s even possible to throw an error again with this throw statement.

It is good coding practice to add details to exceptions that are returned to provide additional info in the debugging procedure.

How to handle System ArgumentOutOfRangeException in c#?

If it contains no arguments, or a string that passed a method if the argument was not passed it would throw argumentOutOf Range exception.

You may eliminate this exception by checking String. The length is less than zero and the string must always be empty when calling the I’mNullOrempty method.

How do I fix IndexOutOfRangeException in C#?

To correct this mistake verify the returns of the search method prior to executing the array.

using system.Collection.Generic;

Public class examples {
static lists = new lists(); 
public static void Main() string[]Arg;

What is System ArgumentOutOfRangeException?

Remark. ArgumentOutOfRangeException exceptions are thrown whenever a method is invoked and a minimum of the arguments passed by the method are not invalid and contain a value invalid.

How do I know if my index is out of range?

Another method for checking if an array is outside the limits is by using function. It checks if the index reaches a limit for a particular index. If the index is above zero or above array size the result becomes false.

Other useful articles

About the author

By Jamie

My Books