Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Collection using IEnumerable vs ICollection vs IList

I need to design my own custom GenericCollection class. Now i have plenty of options to derive it using IEnumerable, ICollection, and IList, where later offers some added functionalities.

I am little confused that if i go with IEnumerable<T> i might require declaring the object to actually hold the collection like in this case _list.

public class GenericCollection<T> : IEnumerable<T> {     private List<T> _list;     //... } 

But if i go with ICollection<T> or IList<T>, i do not require to declare the List object as it is implicitly available.

public class GenericCollection<T> : IList<T> {     // no need for List object     //private List<T> _list;      //... } 

What is the difference between these two approaches with respect to performance?

In which scenario each one is preferred especially when it comes to designing your own collection. I am interested in the light weight collection with good performance. I think this can be achieved using IEnumerable<T> but how exactly along with some strong reasons to go with it?

I have reviewed some existing posts but none is giving required information.

Returning 'IList' vs 'ICollection' vs 'Collection'

like image 386
Furqan Safdar Avatar asked Oct 31 '12 11:10

Furqan Safdar


People also ask

Should I use ICollection or IList?

IList<T> is essentially an ICollection<T> with random order-based access. In this case you should decide whether or not your results require list semantics such as order based indexing (then use IList<T> ) or whether you just need to return an unordered "bag" of results (then use ICollection<T> ).

Should I use IList or IEnumerable?

You should use IList when you need access by index to your collection, add and delete elements, etc., and IEnumerable when you need just enumerate over your collection.

What is the difference between ICollection and IList?

The IList interface implements both ICollection and IEnumerable interfaces. This interface allows us to add items to and remove items from the collection. It also provides support for accessing the items from the index. This interface has more power than the preceding two interfaces.

What is the difference between IEnumerable and ICollection in C#?

IEnumerable has one property: Current, which returns the current element. ICollection implements IEnumerable and adds few additional properties the most use of which is Count. The generic version of ICollection implements the Add() and Remove() methods.


2 Answers

IEnumerable, ICollection, and IList (generally, any type with an I prefix) are just interfaces. They let you expose what your class will do, but unlike if you inherit a class, interfaces do not provide you a default implementation of any of the things they say you must do.

As far as choosing which interface, here's a quick guide:

  • An IList is an ICollection that can be accessed by index.
  • An ICollection is an IEnumerable with easy access to things like Add, Remove, and Count.
  • An IEnumerable is anything that can be enumerated, even if the list of those things doesn't exist until you enumerate it.

Some classes that you might want to extend (or keep as a private field that runs most of the logic) for your collection are List<T>, Collection<T>, (which implements IList<T>, but with easier access to overriding implementation, see Collection<T> versus List<T> what should you use on your interfaces? for the big differences between these two) ObservableCollection<T>, or collections that are not lists, like Dictionary<T, U> and HashSet<T>. For more info on any of these, look up the MSDN documentation on the class.

like image 130
Tim S. Avatar answered Oct 22 '22 07:10

Tim S.


First off, you don't have to actually choose betwen these interfaces, if it's necessary you can implement all three. Second, implementing IEnumerable does not require you to make the underlying list public. You can implement just the methods to use the Enumerator of the underlying list.

Performancewise, I doubt there'll be much of an impact, focus on what you need functionally. The only way to know for sure is to measure.

like image 36
Rik Avatar answered Oct 22 '22 06:10

Rik