asp.net C # [],list,array,arraylist differences

Source: Internet
Author: User

ASP tutorials [],list,array,arraylist differences in. NET C #

[] is for a specific type, fixed length.

The List is for a specific type, arbitrary length.

The List class is the generic equivalent class of the ArrayList class, and in some cases it is easier to use than arrays and ArrayList.

We assume that there is a set of data in which each of the data is a struct.

public struct Item
{
public int Id;
public string DisplayText;
}
Note that the structure is not assignable to the instance field, that is, public int Id = 1 is incorrect.

Using System.Collections.Generic;

list<item> items = new list<item> ();

Add to
Item item1 = new Item ();
Item1. Id = 0;
Item1. DisplayText = "Mercury";
Items. ADD (ITEM1);

Add to
Item ITEM2 = new Item ();
Item2. Id = 1;
Item2. DisplayText = "Earth";
Items. ADD (ITEM2);

Modify
The structure is used here, so it cannot be used directly with items[1]. DisplayText = "Venus"; if Item is a class, you can use it directly. Why, then? Because the structure is passed by value.

Item item = Items[1];
Item. DisplayText = "Venus";
ITEMS[1] = Item;

Array is for any type, fixed length.

private void Change (array array)
{
for (int i = 0; i < array. Length; i++)//Use Length
{
Array. SetValue (int) array. GetValue (i) * 2, I); Type conversion required
}
}

The ArrayList is for any type, arbitrary length.


Unlike arrays, the types of ArrayList elements can be different.

declaring objects

There are three kinds of overloaded methods for declaring ArrayList, and there are two kinds of commonly used
ArrayList al = new ArrayList ();
ArrayList al = new ArrayList (3);
In the example above, the parameter value 3 indicates the capacity, that is, how many elements can be accommodated


Array and ArrayList are implemented in any type by storing object, so they are converted when used.

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.