A deep analysis of the difference between ilist<t> and list<t> in C #

Source: Internet
Author: User
When writing the code:
IList IList11 =new List ();
List List11 =new list ();

Some doubts, so search on the internet, very inspired, so the collection, but to some point of view, with the red font mark my Views!

First, the IList generic interface is a descendant of the ICollection generic interface and is the base interface for all generic lists.
It is only the interface of all generic types, and there are not many ways to be convenient and practical, if only as the bearer of the collection data, indeed, IList can be competent.
However, more often, we want to process the collection data, filter the data, or sort it. This time, the IList will be helpless.

1, when you only want to use the interface method,ilis<> this way is better. He does not acquire other methods and fields that implement this interface, effectively saving space. (Since the subclass is inheriting the subclass of the parent class and has its own properties and methods, then the subclass new comes out should have and must have, whether placed in the parent class variable inside or the variable of its own type, otherwise the upward transformation of the data will be lost, it is terrible!) )

2, IList <> is an interface, defines a number of operating methods to implement these methods for you, List <> is a generic class, it has implemented the IList <> defined methods

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

These two lines of code, from the operational point of view, are actually creating an instance of a list object, which means that their operation is not different.
Only the return value variable type used to hold this operation is different.
So, we can understand that these two lines of code are different in purpose.
List List11 =new list ();
is to create a list, and you need to use the function of the list to do related operations.
And the IList IList11 =new List ();
Just want to create an instance of an object based on the interface IList, but this interface is implemented by the list. So it just wants to use the functionality specified in the IList 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.