一、Array(数组)
1、申明时必须要指定数组长度。
2、数据类型安全。
申明数组如下:
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 }
二、ArrayList
1.可以动态扩充和收缩对象大小。
2.但是类型不安全(需要装箱拆箱消耗性能)。
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 }
三、List
结合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.html