Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between List and IList in C#?
The main difference between List and IList in C# is that List is a concrete class that implements the list functionality, while IList is an interface that defines the contract for list operations. The IList interface inherits from both ICollection and IEnumerable interfaces.
List<T> and IList<T> both represent collections of objects that can be accessed by index. They provide methods to insert, remove, search, and sort elements. The key distinction is that List<T> is a specific implementation while IList<T> is a contract that multiple classes can implement.
Key Differences
| List<T> | IList<T> |
|---|---|
| Concrete class with implementation | Interface defining contracts |
| Can be instantiated directly | Cannot be instantiated directly |
| Has additional methods like FindAll(), Sort() | Contains only essential list operations |
| Less flexible for polymorphism | More flexible, allows different implementations |
Syntax
Following is the syntax for declaring List<T> and IList<T> −
// Direct List instantiation List<string> list = new List<string>(); // IList reference to List implementation IList<string> ilist = new List<string>();
Using IList Interface (Invalid Example)
This example shows why you cannot instantiate an interface directly −
using System;
using System.Collections.Generic;
class Demo {
static void Main(string[] args) {
try {
// This will cause a compilation error
// IList<string> ilist = new IList<string>();
Console.WriteLine("Cannot instantiate an interface directly");
Console.WriteLine("IList is a contract, not an implementation");
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Cannot instantiate an interface directly IList is a contract, not an implementation
Using List and IList Correctly
This example demonstrates proper usage of both List<T> and IList<T> −
using System;
using System.Collections.Generic;
class Demo {
static void Main(string[] args) {
// IList reference pointing to List implementation
IList<string> ilist = new List<string>();
ilist.Add("Mark");
ilist.Add("John");
// Direct List instantiation
List<string> list = new List<string>();
list.Add("Alice");
list.Add("Bob");
Console.WriteLine("IList contents:");
foreach (string item in ilist) {
Console.WriteLine(item);
}
Console.WriteLine("List contents:");
foreach (string item in list) {
Console.WriteLine(item);
}
// List has additional methods not in IList
list.Sort();
Console.WriteLine("Sorted List:");
foreach (string item in list) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
IList contents: Mark John List contents: Alice Bob Sorted List: Alice Bob
When to Use List vs IList
Use IList<T> when you want flexibility and polymorphism − your method can accept any class that implements IList<T>. Use List<T> when you need specific List functionality like Sort(), FindAll(), or BinarySearch().
Conclusion
List<T> is a concrete implementation while IList<T> is an interface contract. Use IList<T> for flexible, polymorphic code and List<T> when you need specific List methods. Both provide indexed access to collections but serve different architectural purposes.
