0

MS source code here

In .net List<T> class inherit two interface (IList<T> and System.Collections.IList)

And it implement two method add method

public void Add(T item) {
        if (_size == _items.Length) EnsureCapacity(_size + 1);
        _items[_size++] = item;
        _version++;
    }

int System.Collections.IList.Add(Object item)
    {
        ThrowHelper.IfNullAndNullsAreIllegalThenThrow<T>(item, ExceptionArgument.item);


        try { 
            Add((T) item);            
        }
        catch (InvalidCastException) { 
            ThrowHelper.ThrowWrongValueTypeArgumentException(item, typeof(T));            
        }

        return Count - 1;
    }

first method is public, second is private method.

IList have no add method, but System.Collections.IList did

What is the concept of inherit from System.Collections.IList?

Community
  • 1
  • 1
Max CHien
  • 133
  • 1
  • 8
  • 2
    Just to be clear, classes implement interfaces and inherit from base classes. Classes do not inherit interfaces. (Interfaces can inherit from base interfaces though). – DeanOC Apr 07 '16 at 02:45
  • 3
    `List` does not *inherit* from `IList` and `IList`, it *implements* them, because they are interfaces, not classes. You can implement as many interfaces as you like - this is not "multiple inheritance". – Blorgbeard Apr 07 '16 at 02:45
  • 1
    The second is not a private, it is just an explicit implementation of the System.Collections.IList.Add method – Thomas Apr 07 '16 at 02:47
  • "What is the concept of inherit from `System.Collections.IList`?" - It is so that `List` may be used in any place that a type of `System.Collections.IList` is required - this is particularly important when working with non-generics code. It's as simple as that. – Enigmativity Apr 07 '16 at 03:23

1 Answers1

0

First of all, as others have already pointed out, classes implement interfaces not inherit them.

int System.Collections.IList.Add(Object item)

Here the class is explicitly implementing the Add() method of the interface. Contrary to implicit interface implementation, such methods can are accessible only using a interface reference not the class reference. Refer here for detailed difference between implicit and explicit interface implementation here

Community
  • 1
  • 1
csh
  • 7
  • 2