Search code examples
c#interfacec#-2.0explicit-interface

how List<T> does not implement Add(object value)?


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?


Solution

  • 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.