- Array 类是 C# 中所有数组的基类,它是在 System 命名空间中定义。Array 类提供了各种用于数组的属性和方法。它是一个抽象类
// 摘要: // 提供一些方法,用于创建、处理、搜索数组并对数组进行排序,从而充当公共语言运行时中所有数组的基类。 [Serializable] [ComVisible(true)] public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
- 简单数组在定义时必须指定数据类型和大小。
int[] myArray = new int[4]; StudentModel[] students=new StudentModel[5];
- ArrayList在定义时可以不指定数据类型和大小。
ArrayList list = new ArrayList(); //新增数据 list.Add("abc"); list.Add(123);
因为ArrayList在插入值时都当成object类型来操作,所以ArrayList不是类型安全的。在存储和检索值类型时通常会发生装箱和拆箱操作,从而带来性能耗损。
- List在定义时可以不指定大小,但必须指定数据类型。
List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3);
因为List在定义是指定了类型,所以存储和检索值类型时没有发生装箱和拆箱操作,从而在性能上要好于ArrayList.