I have added a new class to my library which will be part of the public documented API. The underlying data structure is a native array though List<T>
is used when first generating the native array.
The MSDN indicates that List<T>
throws ArgumentOutOfRangeException
rather than IndexOutOfRangeException
(which can be thrown when accessing a native array).
So I plan to update my functions to always throw ArgumentOutOfRangeException
for consistency within my documented API like follows:
public class MyClass {
private int[] _values;
public int GetValue(int index) {
if (index < 0 || index >= _values.Length)
throw new ArgumentOutOfRangeException("index");
return _values[index];
}
}
My question is this:
Will the above source incur two range checks (my one + native array one) or is the .NET compiler smart enough to remove the IndexOutOfRangeException
checks?