Why does .NET FCL implement a generic and non-generic IList and IList <T> interface in the List <T> class at the same time?

The list is implemented as follows in .NET. I'm surprised why they both implemented a common and non-common interface in the classroom.

[Serializable]
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>,
IList, ICollection, IEnumerable {
public List();
public void Add(T item);
public Int32 BinarySearch(T item);
public void Clear();
public Boolean Contains(T item);
public Int32 IndexOf(T item);
public Boolean Remove(T item);
public void Sort();
public void Sort(IComparer<T> comparer);
public void Sort(Comparison<T> comparison);
public T[] ToArray();
public Int32 Count { get; }
public T this[Int32 index] { get; set; }
}

      

+3


source to share


2 answers


Microsoft made it very easy for .NET programmers to forget that COM existed. This, however, is not close to reality, COM is alive and well and is still the main interoperability technology in Windows. As with the recent example, the modern UI in stores and phones is just COM based under the hood. Very carefully hidden, although it is almost impossible to tell that you are actually using COM when you write a C # application. Except for black magic, work effortlessly with code written in Javascript or C ++.

Generics in .NET is an interoperability issue, a pure .NET implementation detail that other non-NET languages ​​know nothing about. Therefore, List<T>

it cannot be [ComVisible(true)]

.



IList, ICollection and IEnumerable to the rescue, interfaces that are ComVisible and usable by code written in almost any Windows language.

+3


source


This is a compatibility issue. There are many components (e.g. in UI libraries) that are not shared IList

because using a shared version is unnecessary and often undesirable here.

Consider this WPF example:



var list = new List<MyDataType>();
var collectionView = new ListCollectionView(list);

      

Let's say it List<T>

doesn't IList

- you just couldn't use it in this example. Moreover, imagine what ListCollectionView

should be common and try to implement it.

+1


source







All Articles