Important Interface in .NET: Work With IList Interface

Introduction

Welcome to the “Important Interface in C#” article series. In this series, we will be talking about various useful interfaces of the .NET class library. In our previous articles we have talked about the IEnumerable, ICollection, and IComparable interfaces. You can read them here.

Important Interface in .NET: Work with IEnumerable Interface

Important Interface in .NET: Work with ICollection Interface

Important Interface in .NET: Work with IComparable Interface

In this article, we will learn the IList interface in the .NET class library. If you are an experienced .NET developer, then you might know how a List collection helps in our day-to-day programming life.

The IList interface represents a non-generic collection that can be accessed by an index.

The IList interface has one generic and another non-generic version, as in the following.

  • IList: without argument
  • IList<T>: argument of type T

The IList interface is located in the .NET class library as in the following.

  • Namespace: System.Collection
  • Assembly: mscorlib.dll

IList<T> is located in the .NET class library as in the following.

  • Namespace: System.Collection.Generic
  • Assembly: mscorlib.dll

Definition of the IList interface

Here is the implementation of the IList interface. We are seeing that the IList interface implements the ICollection and IEnumerable interfaces so that we can use the methods and properties of the ICollection and IEnumerable interfaces from the IList interface.

public interface IList : ICollection, IEnumerable
{
    bool IsFixedSize { get; }
    bool IsReadOnly { get; }
    object this[int index] { get; set; }
    int Add(object value);
    void Clear();
    bool Contains(object value);
    int IndexOf(object value);
    void Insert(int index, object value);
    void Remove(object value);
    void RemoveAt(int index);
}

IList has three properties and seven methods that we can use to manipulate a collection that is derived from the IList interface.

Here is a generic version of the IList interface.

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
    T this[int index] { get; set; }
    int IndexOf(T item);
    void Insert(int index, T item);
    void RemoveAt(int index);
}

It contains one property and three methods.

Ok, now it’s time to implement an example of an IList and an IList<T>.

Since List<T> implements an IList interface, we can initialize an IList with the object of List<T>. Here is a sample implementation.

using System;
using System.Collections;
namespace SelfHostingWebAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            // Implement IList into List with primitive data type as parameter
            IList li1 = new List<int>();
            li1.Add(10);
            IList li2 = new List<string>();
            li2.Add("sourav");
            IList li3 = new List<double>();
            li3.Add(10.10);
            Console.WriteLine(li1[0]);
            Console.WriteLine(li2[0]);
            Console.WriteLine(li3[0]);
            Console.ReadLine();
        }
    }
}

We are passing various arguments instead of T in the List and initiating an IList object. This is the output of the example above.

output2

In the example above, we have passed a predefined data type as the argument/parameter of List<T>, and in this example, we will pass a user-defined data type or object of our own class as the argument. Have a look at the following example.

using System;
using System.Collections;
namespace SelfHostingWebAPI
{
    class Person
    {
        public string name { get; set; }
        public string surname { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Implement IList into List with user-defined data type as parameter
            Person p1 = new Person();
            p1.name = "Sourav";
            p1.surname = "Kayal";
            IList list = new List<Person>();
            list.Add(p1);
            Console.WriteLine(((Person)list[0]).name + " " + ((Person)list[0]).surname);
            Console.ReadLine();
        }
    }
}

implementobject

Let’s implement a few examples using IList<T>,
In this example, we will pass our own object instead of T. Have a look at the following example.

using System;
using System.Collections.Generic;
namespace SelfHostingWebAPI
{
    public class Person
    {
        public Int16 ID { get; set; }
        public string name { get; set; }
        public string surname { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> person = new List<Person>();
            person.Add(new Person { ID = 1, name = "sourav", surname = "kayal" });
            person.Add(new Person { ID = 2, name = "Ram", surname = "Kumar" });
            person.Add(new Person { ID = 3, name = "shyam", surname = "Kumar" });

            foreach (Person p in person)
            {
                Console.WriteLine(p.ID + " " + p.name + " " + p.surname);
            }
            Console.ReadLine();
        }
    }
}

Here is the output of the example above.

Conclusion

In this example, we learned the concept of the IList and IList<T> interfaces that developers use every now and then in application development. In a future article, we will look at a few more interfaces in the .NET class library.


Similar Articles