Differences between IList and List in C #

Source: Internet
Author: User

First, the IList generic interface is the child of the ICollection generic interface and is the base interface of all generic lists.

It is only an interface of all generic types, and there are not many methods that can be used conveniently. If it is only used as the bearer body of the set data, it is true that IList <T> is competent.

However, in more cases, we need to process the set data and filter or sort the data. At this time, IList <T> cannot help.

1. When you only want to use the interface method, ILis <> This method is better. It does not obtain other methods and fields of the class implementing this interface, effectively saving space.

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

 

1 IList <Class1> IList11 = new List <Class1> ();
2 List <Class1> List11 = new List <Class1> ();

 

 

The two lines of code, from the operational point of view, are actually creating a List <Class1> object instance, that is, their operations are no different.

The type of the variable returned for saving this operation is different.

So we can understand that the two lines of code have different purposes.
 

1 List <Class1> List11 = new List <Class1> ();

 

To create a List <Class1> and use the List <T> function, perform related operations.
While

1 IList <Class1> IList11 = new List <Class1> ();

 

You only want to create an instance of Objects Based on the Interface IList <Class1>, but this interface is implemented by List <T>. Therefore, you only want to use the functions specified by the IList <T> interface.

Related Article

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.