IndexOutOfRangeException for IList <T>
I am implementing a list and I am curious about the definition of IndexOutOfRange. Which of the following do you think is better?
/// <exception cref="IndexOutOfRangeException">if index is less than 0
/// or greater than <see cref="Count"/>
public T this[int index] { get { return myArray[index]; } }
or
/// <exception cref="IndexOutOfRangeException">if index outside the valid range
/// for an array of length equal to <see cref="Count"/></exception>
public T this[int index] { get { return myArray[index]; } }
I am thinking of a case where this class would be used from .NET language that indexes arrays starting from 1. I don't know much about this topic, but is the second option better than the first by any means?
source to share
A bit of OT: you can encapsulate a list, not an array, and then everything will drop out in wash, since the encapsulated list will throw appropriate exceptions?
Edit: if you really want an array inside, how do you first wrap it in a list inside an accessory and then select by index. This way the translation from index to array index happens within the language of your component and not from the caller.
Presumably the list indexing semantics don't change in .Net languages? This is what your component should keep track of as it implements the list interface.
Edit again: is this a problem at all?
People access your array through an accessory that is written in a language that you control and therefore know where the array indexing starts from. Even if you call your (say) C # class from the caller of VB.Net, the accessor will still use the C # idea for array indices, right?
source to share
IList <T> already documents the exception (ArgumentOutOfRangeException, not IndexOutOfRangeException). If you implement the interface and don't change the documented behavior, why document it? If you are going to document it at all, it must agree with the interface.
EDIT: Just noticed your implementation (or at least the implication when naming your implementation). I agree with @Dan, you should really base it on a List <T> instead of an array for the implementation to match the interface. With this condition, there is no need to re-document the exception. If you don't change the underlying implementation, then you should catch the IndexOutOfRangeException and map it against the ArgumentOutOfRangeException, the documented interface.
source to share