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?

0


source to share


4 answers


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?

+1


source


Is it possible that you can also have a "Base" property, then you can say:

/// <exception cref="IndexOutOfRangeException">if index is less than <see cref="Base" />

      



If you don't have a Base property, then the second version is better if you have to be careful with 1-based arrays.

0


source


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.

0


source


As an aside - you can always just inherit from Collection<T>

as a starting point ... this provides raw code, allowing you to customize things. You can inherit from List<T>

, but unlike Collection<T>

in List<T>

there are no useful methods virtual

.

0


source







All Articles