C # [], list, array, arraylist differences and Applications

Source: Internet
Author: User

[] Is for a specific type and fixed length.

List is for a specific type and any length.

Array is of any type and fixed length.

Arraylist is for any type and length.

Array and arraylist implement any type by storing objects, so they must be converted during use.

Application Example

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using system. collections; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {// system. int32 is the int [] arr = new int [] {1, 2, 3}; response. write (ARR [0]); // 1 change (ARR); response. write (ARR [0]); // 2 // The life of the List The namespace is system. collections. generic list <int> List = new list <int> (); list. add (1); list. add (2); list. add (3); response. write (list [0]); // 1 change (list); response. write (list [0]); // 2 // The namespace of array is system array = array. createinstance (system. type. getType ("system. int32 "), 3); // array is an abstract class and cannot be created using new array. Array. setvalue (1, 0); array. setvalue (2, 1); array. setvalue (3, 2); response. write (array. getvalue (0); // 1 change (array); response. write (array. getvalue (0); // 2 // The namespace of arraylist is system. collections arraylist = new arraylist (3); arraylist. add (1); arraylist. add (2); arraylist. add (3); response. write (arraylist [0]); // 1 change (arraylist); response. write (arraylist [0]); // 2} private void change (INT [] ARR) {for (INT I = 0; I <arr. length; I ++) {arr [I] * = 2 ;}} private void change (list <int> List) {for (INT I = 0; I <list. count; I ++) // use count {list [I] * = 2 ;}} private void change (array) {for (INT I = 0; I <array. length; I ++) // use length {array. setvalue (INT) array. getvalue (I) * 2, I); // type conversion required} private void change (arraylist) {for (INT I = 0; I <arraylist. count; I ++) // use count {arraylist [I] = (INT) arraylist [I] * 2; // type conversion required }}}

 

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.