IEnumerable,IList,ICollection in C#

. Friday, January 20, 2012
4 comments

Hello Friends, I am writing blog after really a long time due to my busy work schedule, anyway Today I am going to tell you about these interfaces with their uses.
IEnumerable
Its a core interface which is used to iterate over collection of specified type.Mainly it 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
Its a Interface used to Manipulate generic Collections,as it is 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:Which 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.
I will post code later to explain something tricky thing in this hierarchy.

class Collection
{
static void Main(string[] args)
{
//declare array
string[] arystr = new string[] { "Ajay", "Anil", "Ravi", "Vishal", "Ram" };
//Check for IEnumerable
IEnumerable enumstr = from record in arystr select record;
Console.WriteLine("Start for Enumerable");
foreach (string name in enumstr)
{
Console.WriteLine("Names are {0}", name);
}
Console.WriteLine("Ends for Enumerable");

//Check for IList
IList Liststr = (from record in arystr select record).ToList();
Console.WriteLine("Start for List");
Liststr.Add("Rakesh");
foreach (string name in Liststr)
{
Console.WriteLine("Names are {0}", name);

}
Console.WriteLine("Ends for List");

//Check for ICollection
ICollection Collectionstr = (from record in arystr select record).ToList();
Console.WriteLine("Start for Collection");

Console.Write(Collectionstr.Contains("Ram"));
foreach (string name in Collectionstr)
{
Console.WriteLine("Names are {0}", name);

}
Console.WriteLine("Ends for Collection");
Console.Read();
}
}


IList


Interface which is collection of Non-generic type objects and can be accessed by index. Its 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 like


Insert: Insert the given item at specified Index.


RemoveAt:Removes the Item from Specified Index.


IndexOf : retrieves the item from specified Index.


Now Lets take and Example


When I use IL to see the compilation of above Code I get the following:


 image


image




image


image





This was for IEnumerable Which Implements two basic methods MoveNext and GetCurrent.


image


image


image


 image


 image


From IL its very clear that IList also Implements Interface IEnumerable so when I receive array of string inside IList it internally uses movenext and GetCurrent these two methods to iterate over records,and one more is the method Add,which is because its also Implements ICollection.


image


  image


image


ICollection also Implements IEnumerable but I didn’t added IL code for this, only you can see for ICollection is Contains method which is a boolean type as you can see from IL Output.


So among IList,ICollection,IEnumerable the hierarchy is like


IList Implements ICollection Implements IEnumerable.


hope you enjoyed this article. Thanks