【總結】Array、ArrayList、List

1、Array(數組)html

一、申明時必需要指定數組長度數組

二、數據類型安全安全

申明數組以下:post

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5 
 6             Person[] personArray = new Person[3];
 7 
 8             personArray[0] = new Person { ID = 1, Name = "ZS" };
 9             personArray[1] = new Person { ID = 2, Name = "LS" };
10             personArray[2] = new Person { ID = 3, Name = "WW" };
11 
12         }
13     }
14 
15     public class Person{
16 
17         public int ID { get; set; }
18 
19         public string Name { get; set; }
20     }

2、ArrayList性能

1.能夠動態擴充和收縮對象大小。spa

2.可是類型不安全(須要裝箱拆箱消耗性能)。code

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             ArrayList al = new ArrayList();
 6 
 7             al.Add(new Person { ID = 1, Name = "ZS" });
 8             al.Add(new Studen { ID = 2, Name = "LS", Score = 100 });
 9 
10         }
11     }
12 
13     public class Person{
14 
15         public int ID { get; set; }
16 
17         public string Name { get; set; }
18     }
19 
20     public class Studen {
21 
22         public int ID { get; set; }
23 
24         public string Name { get; set; }
25 
26         public float Score { get; set; }
27     }

3、Listhtm

結合Array和ArrayList的特性,List擁有能夠擴充和收縮對象大小,而且實現類型安全。當類型不一致時編譯就會報錯。對象

 1   class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             List<Person> list = new List<Person>();
 6             list.Add(new Person() { ID = 1, Name = "ZS" });
 7             list.Add(new Person() { ID = 2, Name = "LS" });
 8         }
 9     }
10 
11     public class Person{
12 
13         public int ID { get; set; }
14 
15         public string Name { get; set; }
16     }

 

轉載於:https://www.cnblogs.com/Lv2014/p/5695005.htmlblog

相關文章
相關標籤/搜索