C # differences between ArrayList and List (first content blog ),

Source: Internet
Author: User

C # differences between ArrayList and List (first content blog ),

When talking about these two differences, we should first think of a relatively large difference in the nature of these two sets. List is like a train, and ArrayList is like a car in our own house, list has restrictions on loaded things. At the beginning, things must be defined in the generic model, that is, there are almost goods in the trains that pull the goods,

A car is your own car. If you want to pull a car, ArrayList will act as a car, but its relative speed is inferior to that of a train,

In programming, we try not to use ArrayList as much as possible. Because it stores everything, it will often lead to the failure of receiving it with a specific type, because the object type needs to be strongly converted to the type you need during retrieval

Differences between List and ArrayList
1. High list performance and low ArrayList Performance
2. Only types in the generic type can be stored in a list storage. ArrayList stores any types of data at a time.
3. The retrieved values in the List are of the generic type. Because ArrayList has no restrictions on the elements, the system considers the obtained values in the List as data of the object type,
If you want to obtain the content, you need
4. When using List, the using command to be imported is using System. Collections. Generic;
When ArrayList is used, the using command to be imported is using System. Collections;
5. List is a generic set. ArrayList is a non-generic set.

Example:

ArrayList:

ArrayList arr = new ArrayList ();
// Use the Add () method to Add an element. There is no limit on the element type.
Arr. Add (12 );
Arr. Add ("1234 ");
Arr. Add (12.7f );
// Use/subscript/to obtain the element at the specified position
Console. WriteLine ("arr [0] =" + arr [0]);
// Obtain the number of current Arrays
Int count = arr. Count;
// Insert an element to the specified subscript using the insert () method
Arr. Insert (1, "Lao Zhang ");
Console. WriteLine ("arr [1] =" + arr [1]);
// Use the Remove () method to delete a specified element from the array
Arr. Remove ("Lao Zhang ");
Console. WriteLine ("arr [1] =" + arr [1]);
// Delete the element at the specified subscript using the RemoveAt () method
Arr. RemoveAt (0 );
Log (arr );
// Determine whether the specified element exists in the current array
Bool B = arr. Contains ("Old Wang ");
If (B)
{
Console. WriteLine ("Old Wang in the array ");
}
Else
{
Console. WriteLine ("Old Wang is not in the array !!!! ");
}

 

List:

List <string> list = new List <string> ();
// Use the Add () method to Add elements. Only string elements can be added.
List. Add ("123 ");
List. Add ("778 ");

// [Subscript] is used to obtain the elements at the specified position.
Console. WriteLine ("arr [0] =" + arr [0]);
// Obtain the number of elements in the current array
Int count1 = arr. Count;
// Insert an element to the specified subscript using the insert () method
Arr. Insert (1, "Lao Zhang ");
Console. WriteLine ("arr [1] =" + arr [1]);
// Use the Remove () method to delete a specified element from the array
Arr. Remove ("Lao Zhang ");
// Use RemoveAr () to delete the element at the specified subscript
List. RemoveAt (0 );
Console. WriteLine (list [0]);
// Contains () determines whether the specified element exists in the current array
Bool b1 = list. Contains ("Old Wang ");
If (b1)
{
Console. WriteLine ("Old Wang is in the array !!!! ");
}
Else
{
Console. WriteLine ("Old Wang is not in the array !!!! ");
}
// Empty the set
List. Clear ();
Console. WriteLine (list. CounT );

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.