Search code examples
c#genericscollectionsilist

Implementing IList interface


I am new to generics. I want to implement my own collection by deriving it from IList<T> interface.

Can you please provide me some link to a class that implements IList<T> interface or provide me a code that at least implements Add and Remove methods?


Solution

  • Unless you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection<T> since it has everything you need.

    Please note that although implementors of IList<T> are not required to implement this[int] (indexer) to be O(1) (basically, constant-time access), it's strongly recommended you do so.