- 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.