ArrayList al = new ArrayList();
al.Add(1);
al.Add(2);
int[] a = al.ToArray(typeof(int)) as int[];
注意ArrayList.ToArray() 与 ArrayList.ToArray(Type type)的实现是不同的。
public virtual object[] ToArray() { object[] objArray1 = new object[this._size]; Array.Copy(this._items, 0, objArray1, 0, this._size); return objArray1; } |
public virtual Array ToArray(Type type) { if (type == null) { throw new ArgumentNullException("type"); } Array array1 = Array.CreateInstance(type, this._size); Array.Copy(this._items, 0, array1, 0, this._size); return array1; } |