您的位置:首页 > 其它

Array,ArrayList 和 List<T>的选择和性能比较.

2014-01-07 10:53 656 查看

Array Class

Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime

In my opinion,int32[] is an example of Array,so is double[] and so on.for instance:

       Int32[] ints = new int[3];
//ints.GetType()    {Name = "Int32[]" FullName = "System.Int32[]"}    System.Type {System.RuntimeType}

Array a = Array.CreateInstance(typeof(System.Int32), 3);
//a.GetType()        {Name = "Int32[]" FullName = "System.Int32[]"}    System.Type {System.RuntimeType}


 When to use:[b] Array is strongly type,and is work well as parameter,if the the collection is fixed,we should should use Array instead of ArrayList.[/b]

ArrayList Class

[b]  ArrlayList isn't strongly type,every Insertion or Retrial will need a cast to get back to your original type. If you need a method to take a list of a specific type, ArrayLists fall short because you could pass in an ArrayList containing any type,that is,we could contains Object,Int32,Double in the same ArrayList. ArrayLists use a dynamically expanding array internally, so there is also a hit to expand the size of the internal array when it hits its capacity.[/b]
[b]  An ArrayList doesn't use a LinkedList as the internal data structure! It uses a vector which is an array that is dynamically resized on add/insert/delete operations.[/b]
  Actually,most of the time,the operations relevant to Array,such as Copy,IndexOf,Clear and so on.

List<T>

  Most of the time,we use List<T> and it contains all the advantages of Array and ArrayLists. It is strongly typed and it supports a variable length of items.

Ref:

  http://msdn.microsoft.com/en-us//library/system.array(v=vs.110).aspx in msdn

  http://stackoverflow.com/questions/412813/when-to-use-arraylist-over-array-in-c from StackOverflow
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: