SoFunction
Updated on 2025-03-07

Summary of the difference between IList and List in C#

The IList interface and List class are two important types in C# for collection operations.

The differences between them are as follows:

1. Definition and implementation method:

The IList interface is an abstract interface that defines a set of methods and properties used to manipulate lists. It is part of the namespace and can be implemented by other classes.
The List class is a concrete implementation of the IList interface, which provides concrete implementations of all methods and properties defined in the IList interface. The List class is located in the namespace.

2. Generic support:

The IList interface is a non-generic interface that can store objects of any type.
The List class is a generic class that specifies the stored element type and performs type checking at compile time to provide better type safety.

3. Functions and performance:

The IList interface defines a set of basic list operation methods, such as adding, deleting, inserting, index access, etc. It provides basic operational support for lists, but does not provide specific implementations.
The List class provides more functional and performance optimizations based on the IList interface. It uses dynamic arrays to store elements, allowing efficient insertion, deletion, and index access operations. In addition, the List class also provides some additional methods, such as sorting, searching, etc.

Error use cases

using System;
using ;
namespace DemoApplication{
   class Demo{
      static void Main(string[] args){
         IList<string> ilist = new IList<string>();
         //This will throw error as we cannot create instance for an IList as it is an interface.
         ("Mark");
         ("John");
         foreach (string list in ilist){
            (list);
         }
      }
   }
}

The following is the correct case

using System;
using ;
namespace DemoApplication{
   class Demo{
      static void Main(string[] args){
         IList<string> ilist = new List<string>();
         ("Mark");
         ("John");
         List<string> list = new List<string>();
         ("Mark");
         ("John");
         foreach (string lst in ilist){
            (lst);
         }
         foreach (string lst in list){
            (lst);
         }
         ();
      }
   }
}

How to convert List to IList

/// &lt;summary&gt;
/// List to IList public method/// &lt;/summary&gt;
/// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
/// &lt;param name="listObjects"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
protected static IList&lt;T&gt; ConvertToGenericList&lt;T&gt;(IList listObjects)
{
      IList&lt;T&gt; convertedList = new List&lt;T&gt;();
 
      foreach (object listObject in listObjects)
      {
           ((T)listObject);
      }
 
      return convertedList;
}

Summarize:

  • The IList interface is an abstract list operation interface that can be implemented by other classes.
  • The List class is a concrete implementation of the IList interface, providing more functional and performance optimizations.

This is the end of this article about the difference between IList and List in C#. For more related C# IList List content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!