我有一个简单的界面:
public interface IReadOnlyList<T> : IEnumerable<T>
{
T this[int index] { get; }
int Count { get; }
}
我的 API 的用户将被迫使用这个接口,而不是他们通常知道的IList或IEnumerable. 我更喜欢这个,IList因为它只公开可以使用的成员。我不希望所有IsReadOnly Add() Remove() Insert()未使用的垃圾污染我的 API。我更喜欢这个,IEnumerable因为我的用户需要访问索引和计数。这是合理的推理,还是我应该使用更熟悉的IList?为什么?