Pages

Saturday 18 May 2013

Difference between IEnumerable, ICollection and IList Intrefaces in C#

Difference between IEnumerable, ICollection and IList Intrefaces in C#

Below article demonstrates the difference between IEnumerable, ICollection and IList Intrefaces. IEnumerable interface is the core interface and is implemented by ICollection and IList. Lets have a look.

IEnumerable

Its a core interface which is used to iterate over collection of specified type. Mainly implements two methods:

MoveNext: which is of boolean type which tells whether there are more records to move on or not.

GetCurrent: which returns the current record from the collection.
 
ICollection: Implements IEnumerable

It is an interface used to manipulate generic collections, as it implements IEnumerable interface so its obvious that this will also implements methods MoveNext and GetCurrent, so with this interface you can iterate through collection. Apart from this its also having its own methods like:

Add: Adds record at the end of collection
Remove: Removes specified item from collection
Contains: Its a boolean type method which tells whether collection contains the specified item or not.

IList

Interface which is collection of non-generic type objects and can be accessed by index. It is the interface which implements two interfaces ICollection and IEnumerable. So its obvious that this will Implements the methods of the both the interfaces. Its own methods are:

Insert: Insert the given item at specified Index.
RemoveAt: Removes the item from specified Index.
IndexOf: Retrieves the item from specified Index.

No comments:

Post a Comment