List of C #

Source: Internet
Author: User

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 #

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.