How does List <T> not implement Add (object value)?
I think this is pretty stupid and I'm a little confused to ask this question, but I still could not find the answer:
I am considering a class List<T>that implements IList.
public class List<T> : IList
one of the methods included in Ilist,
int Add(object value)
I understand that List<T>I should not expose this method (type security ...), and it really is not. But how can this be? mustnt implement the whole interface?
I believe that this (interface) method is implemented explicitly :
public class List<T> : IList
{
int IList.Add( object value ) {this.Add((T)value);}
}
, Add( object ) . , List<T> IList.
, , , , .
, , , , .
, IList.Add :
, -1 , .
, :
int IList.Add(object value)
{
if (value is T)
{
Add((T)value);
return Count - 1;
}
return -1;
}
This is just an assumption, of course. (If you really want to know for sure, you can always use a Reflector .) This may be a little different; for example, it can generate NotSupportedException, which is often done for incomplete interface implementations, such as an ReadOnlyCollection<T>implementation IList<T>. But since the above meets the documented requirements IList.Add, I suspect this is close to the real thing.