Frederik is right that List<T>
's implementation of IList
is explicit for certain members, particularly those that pose a threat to type safety.
The implementation he suggests in his answer can't be right, of course, since it wouldn't compile.
In cases like this, the typical approach is to make a valiant effort to try to get the interface member to work, but to give up if it's impossible.
Note that the IList.Add
method is defined to return:
The position into which the new
element was inserted, or -1 to
indicate that the item was not
inserted into the collection.
So in fact, a full implementation is possible:
int IList.Add(object value)
{
if (value is T)
{
Add((T)value);
return Count - 1;
}
return -1;
}
This is just a guess, of course. (If you really want to know for sure, you can always use Reflector.) It may be slightly different; for example it could throw a NotSupportedException
, which is often done for incomplete interface implementations such as ReadOnlyCollection<T>
's implementation of IList<T>
. But since the above meets the documented requirements of IList.Add
, I suspect it's close to the real thing.
List<T>
is not an interface, and he's saying thatList<T>
should not expose it. – Ogburn