When writing background programs, IList <T> and List <T> are commonly used. Their respective declarations are as follows:
Here, we use the String class as an example,
(1) IList <String> IList = new List <String> ();
(2) List <String> List = new List <String> ();
For the first time, I felt like (1) (2) was similar, and the difference between them was vague and confused. I went online to check the relevant information and debug it under VS2010. The summary is as follows:
1. IList is a generic interface. Its declaration in the data element is as follows:
Public interface IList <T>: ICollection <T>, IEnumerable <T>, IEnumerable
{
//...
}
2. List is a generic class that implements the interface IList <T>. Its declaration in the data element is as follows:
Public class List <T>: IList <T>, ICollection <T>, IEnumerable <T>, IList, ICollection, IEnumerable
{
//...
}
3. It should be easy to see here,
IList <String> IList = new List <String> ();
Is to define an IList <String> interface type variable, and this interface is implemented by the List class,
List <String> List = new List <String> ();
Is to define a List <String> generic class variable, which is not much different from declaring a common class,
It seems that I still don't know exactly what the difference is. In VS2010 smart sensing, I will be clear about it, you will find that the variable IList can only call the methods defined in the interface IList and the methods inherited by the interface IList from the top of the inheritance chain. The variable IList cannot call the methods in the List class;
In addition to the methods defined in the IList interface, the variable List can also call its own defined methods.