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.