List
Generic collection, which can replace ArrayList
Common methods
ListlistnewList<int>();
Method Name |
function |
ADD () |
adding elements |
AddRange |
Add a Collection |
ToArray () |
To turn a collection into an array |
Array has a method called ToList (), you can turn the array into a set;
Why use list instead of ArrayList
Box packing and unpacking
Converting a value type to a reference type called boxing, converting a reference type to a value type is called unpacking;
Boxing and unpacking are operations at run time, which makes the program run longer, while the generic collection is used to generate the specified type at compile time;
See whether the two types of boxing or unpacking, to see whether the two types of inheritance relationship, there is an inheritance relationship is possible to occur boxing operations;
class name |
element Type |
whether a unboxing occurred |
Run Time |
List |
Determined and unique |
Whether |
relatively short |
ArrayList |
Not sure, but there are many kinds of |
Is |
Long time |
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceList Generic Collection {classprogram {Static voidMain (string[] args) {ArrayListList=NewArrayList ();//arraylist Program time: 00:00:00.1229805 //list Program time: 00:00:00.0276141 //list<int> List = new list<int> (); List. ADD (0);List. ADD (0); Console.WriteLine (List. Count); Stopwatch SW =NewStopwatch (); Sw. Start (); for(inti =0; I <1000000; i++) {List. ADD (i); } SW. Stop (); Console.WriteLine ("program Time:"+SW. Elapsed); Console.readkey (); } }}
List of C #