Questions on specifications of ICollection<T> and IDictionary<K,V>
I tried to implement the System.Collections.Generic.IDictionary<K,V>
interface. The task is not that hard but I have some problems with the
interpretation of the specification of interface members.
1) Exception specification on
System.Collections.Generic.ICollection<T>.CopyTo
1.1) arrayIndex out of range
The specification says that ArgumentOutOfRangeException shall be thrown
in case arrayIndex is less then 0. But if arrayIndex is greater then the
length of array then ArgumentException shall be thrown. Why? Why not
ArgumentOutOfRangeException which seems quite logical.
Is it because if arrayIndex >= array.Length then there is possibility
that the arrayIndex is OK but user passed bad array and bad array of course
should not cause ArgumentOutOfRangeException. So general ArgumentException
is used? Or why?
1.2) array is multidimensional
In such a case ArgumentException should be thrown. But what if parameter
type T is an array type? (Here I am not sure if it is possible and if array
of arrays is treated as a multidimensional array.)
1.3) type casting
ArgumentException shall be thrown in case parameter type T cannot be
cast automaticlly to the type of the array. But how is this ever possible
since the array is of type T[] and collection elements are of type T (or
derived type)?
2) System.Collections.Generic.ICollection<T>.Add respecification in
System.Collections.Generic.Dictionary<K,V>
The specification of System.Collections.Generic.ICollection<T>.Add lists
only one possible exception, NotSupportedException in case of adding to
read-only collection. However the (explicit) implementation in Dictionary<K,
V> adds ArgumentException in case the key already exists.
Perhabs I don't understeand the idea of the specification but I thought
that if we do specify what can be thrown than we don't change it so
drasticly in derived classes.
Adam Badura
Date:Sat, 4 Aug 2007 23:13:36 +0200
Author:
|