C# generic collection and non-generic collection (List ArrayLIst)

ArrayLIst non-generic collection

1. Before using a non-generic collection, you need to pay attention to calling System.Collections
. 2. When using a non-generic collection, there is no clear type requirement for the elements in the collection.
3. A non-generic collection has a dynamic size, which can be based on the amount of data. adjust size

//命名空间
using System.Collections
//创建ArrayLIst集合
ArrayList list = new ArrayList();

//添加元素 可以是任意类型
list.Add();
list.AddRange();//添加数组类型

list.Clear();//清空集合中所有元素
list.Remove();//删除指定元素名元素
list.RemoveAt();//移索引对应的值

list.Insert();//在指定位置插入一个元素
list.InsertRange();//在指定位置插入一个数组

list.Sort();//排序

list.Reverse();//反转

list.Contains();//判断是否包含这个元素


List generic collection

1. Call System.Collections.Generic before using a generic collection
2. Generic collection LIst (strong type), you need to determine the type when using it, <>
3. List performs better and safer than ArrayList in most cases

//引用命名空间
using System.Collections.Generic

//创建泛型集合
List<类型> 集合名 = new List<类型>();
//List<int> list = new List<int>();

list.Add();//添加

/* 计算 */
list.Max();//最大值
list.Min();//最小值
list.Sum();//求和
list.Average();//平均值

/* 删除 */
list.Clear();//清空
list.Remove();//删除指定元素名元素
list.RemoveAt();//移索引对应的值

list.Reverse();//将元素顺序反转

list.Sort();//将元素进行排序

list.Contains();//包含

List<>Comparison with ArrayList

Differences:
1. List< T> Strictly check the type when adding elements, ArrayList can add any type
2. List< T> When adding and reading value type elements, there is no need to box and unbox ArrayList When adding and reading value type elements, Need to pack and unbox

Same point :

1. Access the elements of the collection by index
2. The method of adding objects is the same
3. Delete elements by index

Guess you like

Origin blog.csdn.net/wangwei021933/article/details/109599145