Difference Between IEnumerable, ICollection and IList


    IList
    IList exists in System.Collections Namespace.
    IList is used to access an element in a specific position/index in a list.
    Like IEnumerable, IList is also best to query data from in-memory collections like List, Array etc.
    IList is useful when you want to Add or remove items from the list.
    IList can find out the no of elements in the collection without iterating the collection.
    IList supports deferred execution.
    IList doesn't support further filtering.

    IEnumerable
    IEnumerable exists in System.Collections Namespace. 
    IEnumerable can move forward only over a collection, it can’t move backward and between the  
     items.
    IEnumerable is best to query data from in-memory collections like List, Array etc.
    IEnumerable doesn't support add or remove items from the list.
    Using IEnumerable we can find out the no of elements in the collection after iterating the
    collection.
    IEnumerable supports deferred execution.
    IEnumerable supports further filtering.

ICollection 

The ICollection interface is inherited from the IEnumerable interface which means that any class that implements the ICollection interface can also be enumerated using a foreach loop. In the IEnumerable interface we don't know how many elements there are in the collection whereas the ICollection interface gives us this extra property for getting the count of items in the collection. The ICollection interface contains the following: 
Count Property
IsSynchronized Property
SyncRoot Property
CopyTo Method
The Count property is used for maintaining the count of elements in the list whereas the IsSysnchronized and SyncRoot properties help to make the collection thread-safe. The CopyTo method copies the entire collection into an array.

No comments:

Post a Comment