SoFunction
Updated on 2025-03-07

Summary of the difference between Ilist and list in C#

FAQ:

Ilist <> itself is just a generic interface. Since it is an interface, of course it cannot be instantiated. You can only use the following method.
IList <Class1> IList11 =new List <Class1>();
But why use it like this? Why not just use List:
List <Class1> List11 =new List <Class1>();

What are the benefits of the first usage?

Let's summarize:

Ilist <> is supported in .net2.0

The benefits... For example, human and tiger have the function of walking. You can classify these into the interface. When you have an animal class that includes these two animals, if you want them to walk, you only need to adjust the same method regardless of the other party's type. For specific walking, a human has two legs, but has 4 limbs, and an animal has 4 legs. This is to achieve it in the specific animal class, but when they walk, they all swing 4 limbs and take a certain distance forward (of course, when walking forward). So I think these can be classified as behavioral abstraction. How far can a specific step take depends on the height/length of each specific implementation.

Use IList <Class1> IList11 =new List <Class1>();
Convenient to later modifications, when you are not using List, you want to use other types
Just modify this place
No need to modify too much
 

IList <> is an interface that defines some operation methods. These methods need to be implemented by yourself.

List <> is a type. The methods defined by IList <> have been implemented.

List <Class1> List11 =new List <Class1>();
I want to create a List <Class1>, and I need to use the function of List <T> to perform related operations.
and
IList <Class1> IList11 =new List <Class1>();

I just want to create an instance of an object based on interface IList <Class1>, but this interface is implemented by List <T>. So it just wants to use the functions specified by the IList <T> interface.

The interface is loosely coupled...it is conducive to system maintenance and reconstruction...optimize system process...


Encourage the use of interfaces
This allows the separation of functions and specific implementations
Principles for implementing interface separation
Not what you need to do!