SoFunction
Updated on 2025-03-07

Detailed explanation and difference between Array and ArrayList in C#

Detailed explanation and difference between Array and ArrayList in C#

1. How to use Array

  type[]  typename=new type[size]; 

or

 type[]  typename=new type[]{ }; 

Array type variables must be instantiated at the same time as they are declared (if the initialization must at least initialize the size of the array)

Usually we int[], string[]... in fact, we declare an array

like:

 string [] srt=new string[]{"a","b"};

     int[] a=new int[2]; string [] srt=new string[3];

(1): The type data type must not be missing; and must be unified, not as int[] a=new Array[];

(2): The size of the array cannot be missing, otherwise C# thinks it is an error because the array is a fixed-length piece of memory;

(3): On the right is a bracket [], not ()

Note: array array does not provide add, clear, addRange.. methods, but directly set or get the value.

like:a[0] = 0;  a[1] = 1;

2. Usage of C# ArrayList array:

var arrayList = new ArrayList();

      (1);
      (2);
      (50.0); //Supported in .net 4.0.  Why hasn't you studied it yet      foreach (var array in arrayList)
      {
        (array);
      }

3. The conversion between ArrayList and Array

 var arrayList = new List<int>();
      (1);
      (2);
      (50);

      //Copy the values ​​in the ArrayList array into Array      int[] array1=new int[];
      (array1); //Method 1      int[] array2 = (); //Method 2

4. [The difference between Array and ArrayList]

#1. Array type variables must be instantiated (at least the size of the array is initialized), while ArrayList can be declared first.

like:

int[] array = new array[3];
 or int[] array = {1,2,3};
 or ArrayList myList = new ArrayList();

These are all legal, but it is not possible to use int[] array directly.

#2. Array can only store isomorphic objects, while ArrayList can store heterogeneous objects.

Isomorphic objects refer to objects of the same type. If an array declared as int[] can only store plastic data, string[] can only store character data, except for arrays declared as object[].

ArrayList can store any different types of data (because it stores boxed Object objects, in fact, ArrayList uses a private field such as "object[] _items;" to encapsulate the object)

#3 How to store in CLR hosting pairs

Arrays are always stored continuously, while ArrayList storage is not necessarily continuous.

#4 Initialize the size

The initialization of the Array object must only determine the specified size, and the created array size is fixed.

The size of the ArrayList can be specified dynamically, and its size can be specified at initialization or not specified, which means that the space of the object can be increased arbitrarily.

#5 Array cannot add and delete items at will, while ArrayList can insert and delete items at any location.

5. [Similarity between Array and ArrayList]

#1 has an index, that is, any item can be directly obtained and modified through the index.
#2 The objects they create are placed in the managed heap.
#3 can enumerate itself (because both implement the IEnumerable interface).

6. [Some features of ArrayList]

var arrayList = new List<int>(2);
 ();
      
      int size = 2;
      for (int i = 0; i < size; i++)
      {
        (i);
      }
   
      ("compressed capacity:"+); 

When size is 2, the "current capacity" in the output result is 2.
When size is 3 or 4, "current capacity" is 4,
When the size is 5~8, "current capacity" is 8,
When the size is 9~16, "current capacity" is 16,

Through experiments, we can draw a conclusion, that is, whenever the actual number of objects in ArrayList () exceeds its own Capacity threshold, the threshold will automatically double.

 ArrayList myList = new ArrayList(5);

      for (int i = 0; i < 3; i++)
      {
        (i);
      }
      ("actual capacity:" + );
      ();
      ("compressed capacity:" + );
      
      ();

Output:

actual capacity:5
compressed capacity:3

Thank you for reading, I hope it can help you. Thank you for your support for this site!