Understand IList and IList<T> interface in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 41972 red flag
Rating: 5 out of 5  
 1 vote(s)

How to use IList and IList<T> interface in C#

Understand IList and IList<T> interface in C#

In this article we will try to understand IList and IList<T>  in C#.  IList and IList<T> both are interface of C# collection.

Understand IList

IList is non-generic collection object that can be individually access by index. The IList interface has implemented from two interfaces and they are ICollection and IEnumerable. The definition of IList interface is like below.

Public interface IList : ICollection , IEnumerable

We can use the interface to implement another collection. For example we can instantiate IList object to array. Try to understand below code.

    class Program
    {
        static void Main(string[] args)
        {
            IList ilist = new int[10];
            ilist.Add(100);
            Console.ReadLine();
        }
    }

We have used object of IList interface to define integer array. By nature IList object always takes object. So, even if we try to initialize an integer value, it will store as object. In VisualStudio we can check it.


As IList takes object, we cannot add value type into it. Like below example.

    class Program
    {
        static void Main(string[] args)
        {
            IList ilist = new List<string>();
            ilist.Add(100);
            Console.ReadLine();
        }
    }

In this example we are trying to insert integer value into IList. It will throw error like below.


Methods of IList

There are many methods in IList interface. Few of them are.

Add: - To add new object into IList

Clear: - To remove all the items from IList

Insert: - Insert one new object into IList collection to specific location.

Remove: - Remove the first occurrence of a specific object from the IList

RemoveAt: - To remove item at particular object from IList.

 

Properties of IList

Few popular property of IList interface is given below.

Count:-  It will return number of object in List

IsReadOnly:- This property will indicate whether the IList is read-only or not.

Item:- It will return the particular Item of specified index.

Understand IList<T>

IList<T>  interface is very similar with IList the only difference is that it is generic in type. We and pass any type in place of T. Have a look on below code.

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<string> List = new List<string>();
            List.Add("Sourav");
            List.Add("Ram");
            List.Add("Shyam");
 
            foreach (string name in List)
            {
                Console.WriteLine(name);
            }
            Console.ReadLine();
        }
    }
}

Here we are passing string in place of String. The sample output is given below.


As we said earlier that T can be any type, in another example we will pass object of one class as parameter of IList<T> interface. The below code implementation is very similar with above.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Client
{
    class Person
    {
        public string Name { get; set;}
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> List = new List<Person>();
            Person p = new Person();
            p.Name = "Sourav";
            List.Add(p);
            p = new Person();
            p.Name = "Ram";
            List.Add(p);
 
            foreach (Person O in List)
            {
                Console.WriteLine(O.Name);
            }
 
            Console.ReadLine();
        }
    }
}

In this example we are storing object of Person class into IList<T>. Output of this example is given below.

 

Conclusion:-

In this article we have tried to understand IList and IList<T> with example.Hope you have understood the difference between them.


Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)