ASP.NET, Best Practises, Coding

Using IEnumerable vs ICollection vs IList vs IDictionary

In this article we will discuss four important Interfaces used heavily in .NET development.

  • IEnumerator
  • IList
  • IDictionary
  • ICollection

All these are nothing but interfaces, but they are very important in terms of implementing encapsulation on collections, providing decoupled collections in tired architecture like 2 tier or 3 tier and also useful in implementing object oriented concepts like encapsulation, polymorphism.

First important point is that all these are interfaces, so all the qualities of interfaces like decoupling, polymorphism can be benefited.

There are two big advantages of using these in terms of encapsulation and polymorphism.

Encapsulation: We can control how much Collection access should be given to end client.Suppose there is 2 tier architecture and you are exposing array list from business layer to UI OR windows client. With this we can impose restrictions to the window forms UI not to provide add/remove methods to modify the data.

Polymorphism: You can dynamically point to any collections at run time.
Let us take an example that will show how to implement these concepts using various interfaces discussed above.

First we will demonstrate encapsulation using these interfaces.
Create a windows form application and for the same solutions add a new class library project that acts as a middle layer say applicantClassLibrary. Let’s consume middle class library in windows form by adding the class library as a reference to windows UI. We expose the tempArrayList Collection from applicant class to windows forms UI which will display the collection on UI.
Create an applicant class, add some properties and a method LoadApplicantData (), which will return arrayList type to FormUI.

In windows form UI, add one list box control as shown in the above snapshot 1. Let’s fill listbox with the list of items sent by the class library of middle tier. LoadApplicantData () method of Applicant class, is given below:

public class Applicant	
    {
        public string ApplicantName { set; get; }
        public string ApplicantID { set; get; }

        public ArrayList LoadApplicantData()
        {
            ArrayList tempArrayList = new ArrayList();
            Applicant obj1 = new Applicant();
            obj1.ApplicantID = "1001";
            obj1.ApplicantName = "Mahesh";

            tempArrayList.Add(obj1);

            Applicant obj2 = new Applicant();
            obj2.ApplicantID = "1002";
            obj2.ApplicantName = "Kumar";

            tempArrayList.Add(obj2);

            return tempArrayList;
        }
    }

Add the following code snippet to the form load event

       
private void Form1_Load(object sender, EventArgs e)
        {

            Applicant obj1 = new Applicant();
            ArrayList temp1 = obj1.LoadApplicantData();

            foreach (object o1 in temp1)
            {
                Applicant ob = (Applicant)o1;
                lstboxdemo.Items.Add(ob.ApplicantName);

            }
        }

Click on F5 and debug .The form will display the items that are sent from the middle class library.

But if you observe carefully, there is a violation of encapsulation in the code provided that the UI is just view only interface. The business object is exposing the arrayList to FormUI only to display the items. However the UI developer can add his own applicant object and has a provision to modify the data as Shown in the below snapshots.

Using Interfaces

Using Interfaces

Now I want to encapsulate my collection so that it should not allow the UI developer to add /Delete items by restricting the add/Remove methods. We have IEnumerable interface that serves for this purpose.

Rather than exposing the arrayList completely as shown in previous example, let’s expose this list using IEnumerable interface.
This interface allows you to browse through the list, but it will not allow add and remove method on to the list.
Now let’s modify the code as shown below.

Instead of arrayList, return IEnumerable from the business object as shown below.

       
public IEnumerable LoadApplicantData()
        {
            ArrayList tempArrayList = new ArrayList();
            Applicant obj1 = new Applicant();
            obj1.ApplicantID = "1001";
            obj1.ApplicantName = "Mahesh";

            tempArrayList.Add(obj1);

            Applicant obj2 = new Applicant();
            obj2.ApplicantID = "1002";
            obj2.ApplicantName = "Kumar";

            tempArrayList.Add(obj2);

            return (IEnumerable)tempArrayList;

Modify the code of FORMUI and change the type of temp1 objet to IEnumerable as shown below.

IEnumerable temp1 = obj1.LoadApplicantData();

Now the FormUI doesn’t allow you to modify the list that has come from the business object, as it won’t provide any add or remove method to the developer to modify.

IEnumerable interface will only help you to browse through the collection. It will restrict add, remove and other functionalities so that you can implement encapsulation over the collection.

Let us display how many items are there in listbox. The IEnumerable interface does not provide count property.

Now add a label to the FormUI that displays the number of elements present in the list box.

Modify the LoadApplicantData method such that it returns Icollection data as shown below.

public ICollection LoadApplicantData()
        {
            ArrayList tempArrayList = new ArrayList();
            Applicant obj1 = new Applicant();
            obj1.ApplicantID = "1001";
            obj1.ApplicantName = "Mahesh";

            tempArrayList.Add(obj1);

            Applicant obj2 = new Applicant();
            obj2.ApplicantID = "1002";
            obj2.ApplicantName = "Kumar";

            tempArrayList.Add(obj2);

            return (ICollection)tempArrayList;
        }

The corresponding FormUI code is shown in the below snapshot.

Using Interfaces

You can see in the above snapshot, use of Icollection allows you, not only to browse through the list but also can get the count of elements in the list. Click on F5 gives the list of items and also count of items on the output Window.

There are some scenarios where we have to provide the full control to UI so that the list can be modified either by adding new elements to the list or by deleting ,in addition to browse through list and count of list items. We can use either IList OR IDictionary in these scenarios.

IList is used for arrayList and IDictionary is for hash table.

Now modify the LoadApplicantData such that it returns IList data as shown below.

    
public IList LoadApplicantData()
        {
            ArrayList tempArrayList = new ArrayList();
            Applicant obj1 = new Applicant();
            obj1.ApplicantID = "1001";
            obj1.ApplicantName = "Mahesh";

            tempArrayList.Add(obj1);

            Applicant obj2 = new Applicant();
            obj2.ApplicantID = "1002";
            obj2.ApplicantName = "Kumar";

            tempArrayList.Add(obj2);

            return (IList)tempArrayList;
        }

In form load event of FormUI, change the type of temp1 from Icollection to IList. A careful observation reveals that IList allows you to browse through the items, get the count and also provides add and clear method in order to modify the data items. The below snapshot depicts the use of IList.

Using Interfaces

Similarly we can use IDictionary for hash table where in, use hash table instead of arrayList in the class library and use IDictionary as return type. It also provides the same functionalities as of IList, but the only difference is that it is used with Hash tables instead of arrayList.

Finally we can conclude the usage of these interfaces as follows.

IEnumerable: only to browse through the list.

Icollection: Browse through list + to Get counts.

IList: Browse through list+ Get count + add/remove method. (Used with arrayList)

IDictionary: same as IList, but used with hash tables instead of arrayList.

If you enjoyed this post, please consider sharing, leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.