在.NET中,列表的实现方式如下。我很惊讶为什么他们同时在类中实现了通用和非通用接口。
[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; }
}
IList
和IList<T>
并非可互换使用,可以说它们之间并不相互包含。 - MaartenIList
方法和属性是显式实现,因此只有在将列表显式类型化为IList
时才会使用它们。这主要是为了向后兼容性,以确保您可以在使用早于 .NET 泛型之前制作的库时使用通用的List<T>
:) - Luaan