Differences between IList and List

Source: Internet
Author: User

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.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.