5

I believe it's pretty stupid, and I am a bit embarrassed to ask this kind of question, but I still could not find the answer:

I am looking at the class List<T> , which implemetns IList.

public class List<T> : IList

one of the methods included in Ilist is

int Add(object value)

I understand that List<T> should not expose that method (type safety...), and it really does not. But how can it be? mustnt class implement the entire interface?

ET.
  • 1,899
  • 2
  • 18
  • 28

6 Answers6

10

I believe that this (interface) method is implemented explicitly:

public class List<T> : IList
{
     int IList.Add( object value ) {this.Add((T)value);}
}

By doing so, the Add( object ) method will by hidden. You'll only able to call it, if you cast the List<T> instance back to an IList instance.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • You're right about the explicit implementation. But you should fix your example ("recursion" and "can't compile" keep popping up in my mind)... :) – rsenna Dec 08 '10 at 14:59
3

A quick trip to reflector shows that IList.Add is implemented like this:

int IList.Add(object item)
{
    ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);
    try
    {
        this.Add((T) item);
    }
    catch (InvalidCastException)
    {
        ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));
    }
    return (this.Count - 1);
}

In other words, the implementation casts it to T to make it work and fails it you pass a non T compatible type in.

Sean
  • 60,939
  • 11
  • 97
  • 136
2

List<T> explicitly implements IList.Add(object value) which is why it's not typically visible. You can test by doing the following:

IList list = new List<string>();
list.Add(new SqlDataReader()); // valid at compile time, will fail at runtime
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

It implements it explicitly, so you have to cast to IList first to use it.

List<int> l = new List<int>();
IList il = (IList)l;
il.Add(something);
Femaref
  • 60,705
  • 7
  • 138
  • 176
1

You can call it be casting your list instance to the interface first:

List<int> lst = new List<int>();
((IList)lst).Add("banana");

And you'll get as nice, runtime, ArgumentException.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
1

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.

Community
  • 1
  • 1
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • 1
    The actual implementation (at least in .NET4) throws an `ArgumentException` if you attempt to add an item of the wrong type. (And, if I recall correctly, there was a small bug prior to .NET4 where trying to add `null` to a `List>` via `IList.Add` would also erroneously throw the exception.) – LukeH Dec 08 '10 at 15:16