.netstandardsapi-design

Is it better to use a custom interface or well-known standard interface in an API?


I have a simple interface:

public interface IReadOnlyList<T> : IEnumerable<T>
{
    T this[int index] { get; }
    int Count { get; }
}

Users of my API will be forced to use this interface rather than ones they commonly know like IList or IEnumerable. I prefer this over IList because it only exposes members which can be used. I don't want all that IsReadOnly Add() Remove() Insert() unused junk polluting my API. And I prefer this over IEnumerable because my users will need access to an index and count. Is this sound reasoning, or should I just be using the more familiar IList? Why?


Solution

  • That would be a better solution than implementing IList if your type would not actually implement all the members if IList. You could implement IList and throw NotImplementedException but then you'd be violating Liskov.