List of c #
List
Generic set, which can replace ArrayList
Common Methods
List
list = new List
();
Method Name |
Function |
Add () |
Add Element |
AddRange |
Add a set |
ToArray () |
Convert a set to an array |
An array has a method called ToList (), which can be used to convert an array into a set;
Why use list instead of ArrayList?
Packing and unpacking
Converting the value type to the reference type is called packing, and converting the reference type to the value type is called unpacking;
Packing and unpacking will be a running operation, which will take a long time for the program to run, and the generic set is generated at the Compilation Time of the specified type;
Check whether the two types are packed or unpacked. check whether there is an inheritance relationship between the two types. If there is an inheritance relationship, the packing operation may occur;
Class Name |
Element type |
Disassembly box? |
Running time |
List |
Sure and unique |
No |
Relatively short |
ArrayList |
Not sure, there are many |
Yes |
Long time |
Using System; using System. collections; using System. collections. generic; using System. diagnostics; using System. linq; using System. text; using System. threading. tasks; namespace List generic set {class Program {static void Main (string [] args) {ArrayList list = new ArrayList (); // ArrayList Program time: 00: 00: 00.1229805 // list program time: 00: 00: 00.0276141 // List
List = new List
(); List. add (0); list. add (0); Console. writeLine (list. count); Stopwatch sw = new Stopwatch (); sw. start (); for (int I = 0; I <1000000; I ++) {list. add (I);} sw. stop (); Console. writeLine ("program time:" + sw. elapsed); Console. readKey ();}}}