集合:可以使用集合來維護對象組。
C#中的數組實現為 System.Array 類的實例,它們只是集合類(Collection Classes)中的一種類型。集合類一般用於處理對象列表,其功能比簡單數組要多,功能大多是通過實現 System.Collections 名稱空間中的接口而獲得的,
因此集合的語法已經標准化了。這個名稱空間還包含其他一些有趣的東西,例如,以與 System.Array 不同的方式實現這些接口的類。集合的功能(包括基本功能,例如,用[index]語法訪問集合中的項)可以通過接口來實現,
該接口不僅沒有限制我們使用基本集合類,例如 System.Array,相反,我們還可以創建自己的定制集合類。這些集合可以專用於要枚舉的對象(即要從中建立集合的對象)。這麼做的一個優點是定制的集
合類可以是強類型化的。也就是說,從集合中提取項時,不需要把它們轉換為正確的類型。
System.Collections 命名空間中的幾個接口提供了基本的組合功能:
IEnumerable 可以迭代集合中的項。
ICollection(繼承於 IEnumerable)可以獲取集合中項的個數,並能把項復制到一個簡單的數組類型中。
IList(繼承於 IEnumerable 和 ICollection)提供了集合的項列表,允許訪問這些項,並提供其他一些與項列表相關的基本功能。
IDictionary(繼承於 IEnumerable 和 ICollection)類似於 IList,但提供了可通過鍵值(而不是索引)訪問的項列表。
System.Array 類實現 IList、 ICollection 和 IEnumerable,但不支持 IList 的一些更高級的功能,它表示大小固定的項列表。
ArrayList基本使用方法如下:
1 static void Main(string[] args)
2 {
3 //arrayList容量為10
4 ArrayList arrayList1 = new ArrayList(10);
5
6 // arrayList1[0] = 1;//此時不能通過索引來訪問ArrayList否則會拋出 System.ArgumentOutOfRangeException
7 //給ArrayList添加元素
8 arrayList1.Add(1);
9 arrayList1.Add(2);
10 arrayList1.Add(3);
11 arrayList1.Add(4);
12 arrayList1.Add("hello");
13 arrayList1.Add("ArrayList");
14
15 arrayList1[2] = 4;//這裡可以通過索引方式訪問ArrayList
16
17 Console.WriteLine("arrayList1 count: {0}", arrayList1.Count);
18
19 foreach (object i in arrayList1)
20 {
21 Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());
22 }
23
24 Console.WriteLine("remove item at index 4");
25 arrayList1.RemoveAt(4);
26
27 foreach (object i in arrayList1)
28 {
29 Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());
30 }
31
32 Console.WriteLine("remove item 2");
33 arrayList1.Remove(2);
34
35 foreach (object i in arrayList1)
36 {
37 Console.WriteLine("arrayList1 item {0} type:{1}", i, i.GetType());
38 }
39
40 Console.WriteLine("\n\n");
41 //通過ArrayList1創建ArrayList2
42 ArrayList arrayList2 = new ArrayList(arrayList1);
43 Console.WriteLine("arrayList2 count: {0}", arrayList1.Count);
44 foreach (object i in arrayList1)
45 {
46 Console.WriteLine("arrayList2 item {0} type:{1}", i, i.GetType());
47 }
48
49 Console.WriteLine("\n\nPress any key to exit!");
50 Console.ReadKey();
51 }
運行結果:
arrayList1 count: 6 arrayList1 item 1 type:System.Int32 arrayList1 item 2 type:System.Int32 arrayList1 item 4 type:System.Int32 arrayList1 item 4 type:System.Int32 arrayList1 item hello type:System.String arrayList1 item ArrayList type:System.String remove item at index 4 arrayList1 item 1 type:System.Int32 arrayList1 item 2 type:System.Int32 arrayList1 item 4 type:System.Int32 arrayList1 item 4 type:System.Int32 arrayList1 item ArrayList type:System.String remove item 2 arrayList1 item 1 type:System.Int32 arrayList1 item 4 type:System.Int32 arrayList1 item 4 type:System.Int32 arrayList1 item ArrayList type:System.String arrayList2 count: 4 arrayList2 item 1 type:System.Int32 arrayList2 item 4 type:System.Int32 arrayList2 item 4 type:System.Int32 arrayList2 item ArrayList type:System.String Press any key to exit!
自定義集合
可以通過繼承System.Collections.CollectionBase 類的方式自定義集合,CollectionBase 類有接口 IEnumerable、 ICollection 和 IList,但只提供了一些簡要的實現代碼,特別是 IList 的 Clear()和 RemoveAt()方法,
以及 ICollection 的 Count 屬性。如果要使用提供的功能,就需要自己執行其他代碼。
為便於完成任務, CollectionBase 提供了兩個受保護的屬性,它們可以訪問存儲的對象本身。我們可以使用 List 和 InnerList, List 可以通過 IList 接口訪問項, InnerList 則是用於存儲項的 ArrayList對象。
1 public abstract class Animal
2 {
3 private string name;
4 public string Name
5 {
6 get { return name; }
7 set { name = value; }
8 }
9
10 public Animal()
11 {
12 name = "NULL";
13 }
14
15 public Animal(string newName)
16 {
17 name = newName;
18 }
19
20 public void Feed()
21 {
22 Console.WriteLine("{0} has been fed.", name);
23 }
24 }
25
26 public class Dog:Animal
27 {
28 public void Run()
29 {
30 Console.WriteLine("Dog run....");
31 }
32
33 public Dog(string newName)
34 : base(newName)
35 {
36
37 }
38 }
39
40 public class Bird : Animal
41 {
42 public Bird(string newName)
43 : base(newName)
44 {
45
46 }
47
48 public void Fly()
49 {
50 Console.WriteLine("Bird fly....");
51 }
52 }
53
54 class Animals:CollectionBase
55 {
56 public void Add(Animal animal)
57 {
58 List.Add(animal);
59 }
60
61 public void Remove(Animal animal)
62 {
63 List.Remove(animal);
64 }
65
66 public Animals()
67 {
68
69 }
70
71 //通過下面代碼使得Animals可以通過索引訪問
72 public Animal this[int animalIndex]
73 {
74 get
75 {
76 return (Animal)List[animalIndex];
77 }
78 set
79 {
80 List[animalIndex] = value;
81 }
82 }
83 }
84
85 static void Main(string[] args)
86 {
87 Animals animals = new Animals();
88 animals.Add(new Dog("Jack"));
89 animals.Add(new Bird("Jason"));
90
91 foreach (Animal animal in animals)
92 {
93 animal.Feed();
94 }
95
96 Console.WriteLine("\n\n\nPress any key to exit!");
97 Console.ReadKey();
98 }
運行結果:
Jack has been fed. Jason has been fed. Press any key to exot!
其中, Add()和 Remove()方法實現為強類型化的方法,使用 IList 接口中用於訪問項的標准 Add()方法。該方法現在只用於處理 Animal 類或派生於 Animal 的類,而前面介紹的 ArrayList 實現代碼可處理任何對象。
索引符(indexer)是一種特殊類型的屬性,可以把它添加到一個類中,以提供類似於數組的訪問。this 關鍵字與方括號中的參數一起使用,但這看起來類似於其他屬性。這個語法是合理的,因為在訪問索引符時,將使用對象名,
後跟放在方括號中的索引參數(例如 MyAnimals[0])。