Home  >  Article  >  Backend Development  >  An in-depth analysis of the difference between IList and List in C#

An in-depth analysis of the difference between IList and List in C#

高洛峰
高洛峰Original
2017-01-19 11:51:361351browse

When writing the code, I had some doubts about:
IList IList11 =new List ();
List List11 =new List ();

, so I searched on the Internet and was very inspired. So I saved it, but I disagree with some of the views, so I marked my views in red font!

First of all, the IList generic interface is a descendant of the ICollection generic interface and is the base interface for all generic lists.
It is just an interface for all generic types, and there are not many methods that are convenient and practical. If it is only used as a carrier of collection data, indeed, IList can do the job.
However, more often, we have to process the collection data, filter or sort the data. At this time, IList is helpless.

1. When you only want to use interface methods, ILis<> is better. It does not obtain other methods and fields of the class that implements this interface, effectively saving space. (Since a subclass inherits the parent class and has its own attributes and methods, these should and must be present after the subclass NEW comes out, whether they are placed in the variables of the parent class or in variables of its own type, otherwise If you transform up and then down, the data will be lost, which is terrible!)

2. IList <> is an interface that defines some operation methods that you need to implement yourself. List < ;> is a generic class, which has implemented the methods defined by IList <>

IList IList11 =new List ();
List List11 =new List ();

From an operational point of view, these two lines of code actually create an instance of a List object, that is to say, there is no difference in their operations.
It’s just that the return value variable type used to save this operation is different.
So, we can understand that the purposes of these two lines of code are different.
List List11 =new List ();
I want to create a List, and I need to use the functions of List to perform related operations.
And IList IList11 =new List ();
Just want to create an instance of an object based on the interface IList, but this interface is implemented by List. So it just hopes to use the functions specified by the IList interface

For more in-depth analysis of the differences between IList and List in C#, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn