IEnumerable and ICollection in C#

Introduction

In this section, we will explain two of the most popular collections and their usage. 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.

using System;
using System.Collections.Generic;

namespace Collections
{
    internal class Array
    {
        private static void Main(string[] args)
        {
            var monthsofYear = new List<string>
            {
                "January",
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December"
            };

            foreach (var months in monthsofYear)
            {
                Console.WriteLine(months);
            }
            Console.ReadLine();
        }
    }
}

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

using System;
using System.Collections;
using System.Collections.Generic;

namespace Collections
{
    internal class Array
    {
        private static void Main(string[] args)
        {
            var monthsofYear = new List<string>
            {
                "January",
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December"
            };

            // Casted to ICollection
            ICollection<string> collection = (ICollection<string>) monthsofYear;
            Console.WriteLine(collection.Count);

            Console.ReadLine();
        }
    }
}

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.

using System;
using System.Collections;
using System.Collections.Generic;

namespace Collections
{
    internal class Array
    {
        private static void Main(string[] args)
        {
            string[] monthsofYear =
            {
                "January",
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December"
            };

            // Casted to ICollection
            ICollection<string> collection = monthsofYear;
            (collection as string[])[2] = "Financial Month";

            if (!collection.IsReadOnly)
            {
                collection.Add("Financials");
            }
            else
            {
                Console.WriteLine("Read-only collection");
            }

            foreach (var month in collection)
            {
                Console.WriteLine(month);
            }

            Console.ReadLine();
        }

    }

}

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 their 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 future sections. Until then, stay tuned and happy coding. Also, I hope you guys have tried your hands with Pragmatic WCF, which is free for download now.


Similar Articles