IEnumerable and ICollection in C#

In this section, we will explain two of the most popular collections and their usages. IEnumerable lets you enumerate the collection. Here, it only exposes one member, the GetEnumerator() method. GetEnumerator() returns the enumerator. One important point to understand here is that collections don't iterate themself. It is basically done by an enumerator and the sole purpose of an IEnumerable is to supply an enumerator to the collection. However, foreach works for all the collections. So, if I proceed and implement the following logic, then it will provide the same result.

  1. using System.Collections;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System;    
  5.     
  6. namespace Collections    
  7. {    
  8.     internal class Array    
  9.     {    
  10.         private static void Main(string[] args)    
  11.         {    
  12.             var monthsofYear = new List<string>    
  13.             {    
  14.                 "January",    
  15.                 "February",    
  16.                 "March",    
  17.                 "April",    
  18.                 "May",    
  19.                 "June",    
  20.                 "July",    
  21.                 "August",    
  22.                 "September",    
  23.                 "October",    
  24.                 "November",    
  25.                 "December"    
  26.             };    
  27.     
  28.             foreach (var months in monthsofYear)    
  29.             {    
  30.                 Console.WriteLine(months);    
  31.             }    
  32.             Console.ReadLine();    
  33.         }    
  34.     
  35.     }    
  36.     
  37. }  
properties

Now, ICollection declares an object for an in-memory collection. However, the basic difference between ICollection and IEnumerable is that ICollection is a write operation and IEnumerable is a read-only operation only meant for iteration. However, ICollection uses the following to modify the collection:
  • Add()
  • Remove()
  • Clear()
  • IsReadOnly

Apart from this, it also gives the count of the collection and checks for the containing element with contains(). Now, let me proceed and explain these properties one by one. In the following snippet, I have used the count property of the collection. Apart from that you can use other properties as well, as shown in the following screenshot:

collection

  1. using System.Collections;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System;    
  5.     
  6. namespace Collections    
  7. {    
  8.     internal class Array    
  9.     {    
  10.         private static void Main(string[] args)    
  11.         {    
  12.             var monthsofYear = new List<string>    
  13.             {    
  14.                 "January",    
  15.                 "February",    
  16.                 "March",    
  17.                 "April",    
  18.                 "May",    
  19.                 "June",    
  20.                 "July",    
  21.                 "August",    
  22.                 "September",    
  23.                 "October",    
  24.                 "November",    
  25.                 "December"    
  26.             };    
  27.     
  28.             //casted to ICollection    
  29.             ICollection<string> collection = (ICollection<string>) monthsofYear;    
  30.             Console.WriteLine(collection.Count);    
  31.                
  32.             Console.ReadLine();    
  33.         }    
  34.     
  35.     }    
  36.     
  37. }   
contains
For Lists we can directly get the count using the count property on the variable and by using length on the array variable, but the intention was to show the Collection properties here. Now, let's look at this implementation, this is explicit casting.
    1. using System.Collections;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System;  
    5.   
    6. namespace Collections  
    7. {  
    8.     internal class Array  
    9.     {  
    10.         private static void Main(string[] args)  
    11.         {  
    12.             string[] monthsofYear =   
    13.             {  
    14.                 "January",  
    15.                 "February",  
    16.                 "March",  
    17.                 "April",  
    18.                 "May",  
    19.                 "June",  
    20.                 "July",  
    21.                 "August",  
    22.                 "September",  
    23.                 "October",  
    24.                 "November",  
    25.                 "December"  
    26.             };  
    27.   
    28.             //casted to ICollection  
    29.             ICollection<string> collection = monthsofYear;  
    30.             (collection as string[])[2] = "Financial Month";  
    31.             if (!collection.IsReadOnly)  
    32.             {  
    33.                 collection.Add("Financials");  
    34.             }  
    35.             else  
    36.             {  
    37.                 Console.WriteLine("readonly collection");  
    38.             }  
    39.             foreach (var month in collection)  
    40.             {  
    41.                 Console.WriteLine(month);  
    42.             }  
    43.               
    44.             Console.ReadLine();  
    45.         }  
    46.   
    47.     }  
    48.   

    1.   
    It will produce the following result.

    output
     
    The reason for this is that Arrays are a ReadOnly collection.

    One more interface we have is IReadOnly Collection<T>. Now this property is enumerable and also specifies how many elements it has. Another important interface is IList<T>. In this case elements can be accessed based on its index like string march = monthsofyear[2]; IList<T> is also implemented by Arrays and Lists.

    We'll explain all these interfaces and their implementation in the future sections. Until then stay tuned and happy coding. Also, I hope you guys have tried your hands with Pragmatic WCF that is free for download now.

    Up Next
      Ebook Download
      View all
      Learn
      View all