I am trying to write my own generic collection (List<T>
) in C# but got confused with the interfaces. As I understand, in order to create my own collection, I need to implement several interfaces, like ICollection, IList, IEnumerator, IEnumerable etc. But I can't really understand which ones I need.
Thanks in advance

- 959
- 2
- 12
- 28
-
Depends on what you want to do with the collection. Adding items? Removing items? Searching? Index-based access? Ordering? ... But chances are high you don´t need your own collection-type, as the existing ones should be enough. Or is this just an exercise? – MakePeaceGreatAgain Mar 06 '19 at 10:48
-
"But I can't really understand which ones I need." which interfaces are you going to use? – phuzi Mar 06 '19 at 10:48
-
I wouldn't use `List
`, either, as this could easily be confused with the version already in .Net – phuzi Mar 06 '19 at 10:50 -
You only need to implement IList
... if you do that, you will have already done ICollection – MineR Mar 06 '19 at 10:50, IEnumerable , and IEnumerable. -
This is my homework, to create my own generic collection) – Oleg Ivanytskyi Mar 06 '19 at 10:50
-
Actually you don´t need to impelement **any** interface. So what exactly do you want to do with your collection? – MakePeaceGreatAgain Mar 06 '19 at 10:51
-
@HimBromBeere I need all the things you have listed – Oleg Ivanytskyi Mar 06 '19 at 10:51
-
What about iterating the items? You should provide far more information **into your question**, not into the comments. – MakePeaceGreatAgain Mar 06 '19 at 10:53
-
Thanks everyone for help) – Oleg Ivanytskyi Mar 06 '19 at 10:56
-
Possible duplicate of https://stackoverflow.com/questions/44824519/c-sharp-ienumerable-icollection-ilist-or-list – MakePeaceGreatAgain Mar 06 '19 at 10:56
2 Answers
You have to implement IList<T>
for access to the collection's items by their index. IList<T>
inherits from ICollection<T>
, IEnumerable<T>
and IEnumerable
, so you get those anyway.
For a more basic collection without access to an item by index, you implement ICollection<T>
, which comes with IEnumerable<T>
and IEnumerable
through inheritance.
You can additionally implement IReadOnlyList<T>
(or IReadOnlyCollection<T>
respectively), but that is usually not necessary.
You should also look into inheriting from System.Collections.ObjectModel.Collection<T>
instead, which is the recommended way, because the usual boilerplate interface implementations are already done for you and you can concentrate on the parts that make your collection special.

- 13,731
- 5
- 42
- 55
-
Depending on what OP wants to do with the colelection, implementing `IList` could or could not be neccessary. – MakePeaceGreatAgain Mar 06 '19 at 10:51
-
-
-
I will mark it as an answer in 4 minutes as Stackoverflow doesn't let me do this now) – Oleg Ivanytskyi Mar 06 '19 at 10:56
What is your collection for? If you implement IEnumerable<T>
(which entails implementing IEnumerable
, then you've an enumerable object. If you've also got an Add()
method then you've got an enumerable object that works with collection initialisation. Beyond that, look at the common collection interfaces and ask if they are useful to your use cases, particularly ICollection<T>
(adding and removal in particular) and IList<T>
(indexing in particular).

- 110,372
- 10
- 146
- 251