在.NET Framework 2.0中的System.Collections namespace多了一個1.1所沒有的成員 – System.Collections.Generic
這個namespace最常用的應該是System.Collections.Generic.List
而這個class能做什麼呢?! 其實你把他想成是個物件(或類別)的集合,可能就會比較容易理解
以前在VS6時代,要做物件集合的類別時,常常會使用以下方式來做(以vb6為例)
[vb] ‘ Filename: myClass.cls
Private String m_myValue = “”
Public Property Let myValue(ByVal vData As String)
m_myValue = vData
End Property
Public Property Get myValue() As String
myValue= m_myValue
End Property[/vb]
[vb]’ Filename: myClasses
Private mCol As New Collection
Public Sub Add(cls As myClass)
mCol.Add cls
End Sub
Public Function Insert(ByVal AfterIndexPos As Long, cls As myClass) As Long
If AfterIndexPos > mCol.Count Then Exit Function
If AfterIndexPos = 0 Then
mCol.Add cls, , 1
Insert = 1
Else
mCol.Add cls, , , AfterIndexPos
Insert = AfterIndexPos
End If
End Function
Public Property Get Item(ByVal Index As Long) As myClass
Set Item = mCol(Index)
End Property
Public Property Get Count() As Long
Count = mCol.Count
End Property
Public Sub Remove(ByVal Index As Long)
mCol.Remove Index
End Sub
Public Sub RemoveAll()
Dim i As Long
For i = 1 To mCol.Count
mCol.Remove 1
Next i
End Sub
Private Sub Class_Terminate()
Set mCol = Nothing
End Sub[/vb]
看吧!光一個集合, 就必須寫這麼一大串, 而假如程式內有一堆物件(或類別)集合時, 類似myClasses的程式就要寫好幾個
在.NET Framework 2.0中就不用這麼累了, 只要使用System.Collections.Generic.List
以下就開始來說明System.Collections.Generic.List
[csharp]
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
class2 myText = new class2();
// 使用List.Add()方法來新增集合內容
myText.Values.Add(new class1(“123”));
myText.Values.Add(new class1(“456”));
myText.Values.Add(new class1(“ABC”));
myText.Values.Add(new class1(“DEF”));
myText.Values.Add(new class1(“Jaofeng”));
myText.Values.Add(new class1(“James”));
Predicate FindJaofeng = delegate(class1 obj) {
return obj.Value == “Jaofeng”;
};
Console.WriteLine(string.Format(“Jaofeng位於第 {0} 個”, myText.Values.FindIndex(FindJaofeng)));
Console.WriteLine(“=======================”);
Console.WriteLine(“將所有資料列出”);
int idx = 0;
Action ListAll = delegate(class1 obj) {
Console.WriteLine(string.Format(“第 {0} 個的Value值為 {1}”, idx, obj.Value));
idx++;
};
myText.Values.ForEach(ListAll);
}
}
class class1 {
private string _value = string.Empty;
public string Value { get { return _value; } }
public class1(string value) {
_value = value;
}
}
class class2 {
private List _values = new List();
public List Values { get { return _values; } }
public class2() { }
}
}
[/csharp]
PS:請各位將以上範例的拿掉, 這是iG:Syntax Hiliter的Bug… @@”
執行的結果如下:
Jaofeng位於第 4 個
將所有資料列出
第 0 個的Value值為 123
第 1 個的Value值為 456
第 2 個的Value值為 ABC
第 3 個的Value值為 DEF
第 4 個的Value值為 Jaofeng
第 5 個的Value值為 James
當需要依條件來尋找集合內的某個類別時, 可用List
當需要依條件來尋找集合內的某些類別時, 可用List
當需要依條件來尋找集合內的某個類別的索引值時, 可用List
List
同理, List
當使用List
其內容就是搜尋的判斷式, 如:
[csharp]Predicate FindJaofeng = delegate(class1 obj) {
return obj.Value == “Jaofeng”;
};[/csharp]
return type為boolean值
而上面也有介紹一個List
譬如原本的習慣寫法:
[csharp]foreach (class1 cls in myText.Values) {
// … Do something
}
// 現在變成
Action ActionName = delegate(class1 obj) {
// … Do something
};
myText.Values.ForEach(ActionName);
[/csharp]
程式碼雖然看起來好像沒比較簡化, 但是執行效率卻是比習慣寫法的效率高喔…