Differences between C# arrays, collections and generic collections

Differences between C# arrays, collections and generic collections

The difference between the three:
Array (datatype[ ]): The type of storage is single and cannot be dynamically increased.
Collection (ArrayList): It can store multiple types of elements, and can also be dynamically increased, but consumes a lot of resources.
Generic collection (List<T>): The type of storage is single, but it can be dynamically increased.

array

When an array is declared, its storage type must be specified, and the memory space is allocated directly or indirectly, so dynamic increase cannot be realized.

There are 4 types of array declarations, taking [int] type as an example:
int[] aArray = new int[3];//声明一个整型数组,并使用 new 关键字为其分配3个内存空间
int[] bArray = new int[3] {
    
     1, 2, 3 };
//后面两种比较常用
int[] cArray = new int[] {
    
     1, 2, 3 };
int[] dArray = {
    
     1, 2, 3 };
The length of the array:
int[] myArray = {
    
     0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine(myArray.Length);//数组的长度,输出:10
Array assignment and retrieval:
int[] myArray = new int[10];
myArray[0] = 1;//数组下标从0开始
int temp = myArray[1];
Console.WriteLine(temp);//整型数组元素默认为0,输出:0
Console.ReadKey();

gather

To declare a collection (ArrayList), you must first refer to the System.Collections namespace. When a collection is declared, it is not necessary to specify its storage type. Its memory space will also increase as the number of elements in the collection increases. Therefore, it is very suitable for storing different types of elements. But in the actual development process, it consumes a lot of memory, so it seems very tasteless.

Collection declaration:
ArrayList list = new ArrayList();
Addition of collection elements:
ArrayList list = new ArrayList();
list.Add(1);//存储整数类型
list.Add('1');//存储字符类型
list.Add("1");//存储字符串类型
list.Add(true);//存储布尔类型
list.Add(list);//存储集合类型
list.AddRange(new int[] {
    
     1, 2, 3, 4, 5, 6 });//依次存储整型数组的每个元素
list.AddRange(new char[] {
    
     'a', 'b', 'c' });//依次存储字符数组的每个元素
//存储各种类型...

Note: The parameter type of the [Add] method is Object. Every time a value type element is added, the boxing behavior cannot be avoided, so it consumes a lot of memory.

The number of collection elements and the storage space (variable):
ArrayList list = new ArrayList();
list.Add(1);
Console.WriteLine(list.Count);//集合元素个数,输出:1
Console.WriteLine(list.Capacity);//集合可存储空间,输出:4
//添加更多的集合元素,当元素个数超出可存储空间时,可存储空间翻倍增长
list.Add(1);
list.Add("c");
list.Add('c');
list.Add(true);
Console.WriteLine(list.Count);//集合元素个数,输出:5
Console.WriteLine(list.Capacity);//集合可存储空间,输出:8

Collection storage space growth law: 4 (default), 8, 16, 32, 64, 128, 256, 512 ... ...

Collection reassignment and value:
 ArrayList list = new ArrayList();
 list.Add(2);//添加的元素会自动补充到前面
 Console.WriteLine(list[0]);//输出:2
 list[0] = 1;
 Console.WriteLine(list[0]);//输出:1

Note: The collection element must be after [Add] before the element at a certain position can be reassigned, otherwise an error will be reported.

insert image description here

generic collection

Generic collections (List<T>) must first reference the System.Collections namespace. When declaring a generic collection, its storage type must be specified. Its memory space will also increase with the increase of elements in the generic collection.

Declaration of a generic collection:
List<int> list = new List<int>();
Addition of generic collection elements:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.AddRange(new int[] {
    
     1, 2, 3});
//只能存储int类型

Note: Unlike collections, the parameter type of the generic collection [Add] method is the specified type [T]. Therefore, the boxing and unboxing behavior is avoided.

The number of generic collection elements and the storage space (variable):
List<int> list = new List<int>();
list.Add(1);
Console.WriteLine(list.Count);//泛型集合元素个数,输出:1
Console.WriteLine(list.Capacity);//泛型集合可存储空间,输出:4
//添加更多的整数类型元素,当元素个数超出可存储空间时,可存储空间翻倍增长
list.Add(1);
list.Add(1);
list.Add(1);
list.Add(1);
Console.WriteLine(list.Count);//泛型集合元素个数,输出:5
Console.WriteLine(list.Capacity);//泛型集合元素个数,输出:8

Generic collections are the same as collections, and their storage space grows in the same way.

Reassignment and retrieval of generic collections:
List<int> list = new List<int>();
list.Add(2);//添加的元素会自动补充到前面
Console.WriteLine(list[0]);//输出:2
list[0] = 1;
Console.WriteLine(list[0]);//输出:1

Note: The generic collection is the same as the collection. The elements of the generic collection must be added after [Add] before the element at a certain position can be reassigned, otherwise an error will be reported.

比较泛型集合与数组、集合的特点:
1、泛型集合与数组都只能存储相同类型的元素,但泛型集合可实现动态增加。
2、泛型集合与集合都可以动态增加,但泛型集合只能添加指定的类型元素。
3、泛型集合添加元素不会发生装箱行为,而集合添加元素会发生装箱行为。
4、未发生装箱行为的泛型集合比发生装箱行为的集合,更节省内存资源。

Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.

Guess you like

Origin blog.csdn.net/qq_46051312/article/details/123460868