C # arrays, ArrayList, and lists

Source: Internet
Author: User

In C # arrays, Arraylist,list can store a set of objects.

One, array

Declaration: Data type [] arrayname = new data type [int length];| | data type [] arrayname = new data type []{AA,BB,CC,DD};

Assignment: obj = arrayname[index];arrayname[index] = obj;

Feature: It is continuously stored in memory, so its index speed is very fast, and the assignment and modification elements are simple. The length must be specified .

Application scenario: Used in clear array length and fixed length.

Second, ArrayList

Statement: ArrayList arraylist= new ArrayList ();

Action: Arraylist.add (obj);//Add Object Arraylist.removeat (index),//Remove Object ArrayList. Insert (index, obj);//Insertion Object arraylist[index] = obj;//Assignment | | Modify Value obj = Arraylist[index];//value

Attribute: ArrayList is part of the namespace System.Collections and must be referenced when using the class, inheriting the IList interface, providing data storage and retrieval. The size of the ArrayList object is dynamically expanded and shrunk according to the data stored in it. Therefore, you do not need to specify the length of the ArrayList object when declaring it .

Scenario: Because ArrayList objects within an array do not have a type requirement, there may be objects within the arrays that are not of the same type, that is, ArrayList is not type-safe. Boxing and unboxing operations typically occur when a value type is stored or retrieved, resulting in significant performance wear and tear. So we don't know the length of the array and whether the object data types in the array are uniform.

Third, generic list

Declaration:list<t> arraylist= new list<t> ();

Action: Arraylist.add (obj);//Add Object Arraylist.removeat (index);//Remove Object Arraylist.insert (index, obj);//Insert Object Arraylist[index] = obj; Assign Value | | Modify Value obj = Arraylist[index];//value

Attribute: The list class is a generic equivalent class of the ArrayList class, and most of its usage is similar to ArrayList because the list class inherits the IList interface. The key difference is that when declaring a list collection, we also need to declare the object type for the data in the list collection.

Scenario: We use the array length and the consistency of the object data types within the array.

Write in the following: the capacity of the array is fixed, and the capacity of the ArrayList and list can be modified as required. The list is more cost-efficient than ArrayList when using a value type to make a boxed unboxing operation.

C # arrays, ArrayList, and lists

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.