Writing code today is encountering such a paragraph:
1 IList IList11 =new List (); 2 List List11 =new list ();
Baidu has a lot, the confused on the first note down, do a summary.
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.
2, IList <> is an interface, defines a number of operating methods these methods to you to achieve
List <> is a type of method that has implemented the IList <> definition
List <Class1> List11 =new list <Class1> ();
You want to create a list <class1>, and you need to use the function of list <T> to do the related operation.
While IList <Class1> IList11 =new List <Class1> ();
Just want to create an instance of an object based on the interface IList <Class1>, but this interface is implemented by list <T>. So it just wants to use the functionality specified in the IList <T> interface.
Interface for loose coupling ... facilitates system maintenance and refactoring ... Optimize system flow ...
In addition, provide a DataTable to List<> code:
1 PublicIlist<t> getlist<t>(DataTable table)2 {3ilist<t> list =NewList<t> ();//The principle of the Richter replacement4T t =default(T);5propertyinfo[] Propertypes =NULL;6 stringTempname =string. Empty;7 foreach(DataRow rowinchtable. Rows)8 {9t = activator.createinstance<t> ();////Create an instance of the specified typeTenPropertypes = T.gettype (). GetProperties ();//get the properties of a class One foreach(PropertyInfo Proinchpropertypes) A { -Tempname =Pro. Name; - if(table. Columns.contains (Tempname.toupper ())) the { - ObjectValue =Row[tempname]; - if(Value issystem.dbnull) - { +Value =""; - } +Pro. SetValue (t, value,NULL); A } at } - list. ADD (t); - } - returnlist; -}View Code
where T t = default (t); is to return the default value of T. For example, if the type of T is of type int, then the value of this default (t) is 0, and if it is a string, the return value is "" "empty string.
Reference Blog: 44033823
68059297
The difference between ilist<t> and list<t> in C #