List of c #

Source: Internet
Author: User

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 ();}}}
   
  

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.