Arrays and list both inplement IList<T>
which throws an ArgumentOutOfRangeException
instead of an IndexOutOfRangeException
when you try to access an item at a negative index:
MSDN:
ArgumentOutOfRangeException
: index is not a valid index in theIList<T>
You can reproduce it with this code:
IList<string> test = new string[]{ "0" };
string foo = test[-1]; // ArgumentOutOfRangeException
If you use it as string[]
you get your expected IndexOutOfRangeException
:
string[] test = new string[]{ "0" };
string foo = test[-1]; // IndexOutOfRangeException
That's the reason why it throws the ArgumentOutOfRangeException
of IList<T>
instead of the IndexOutOfRangeException
of array.