在.NET Framework 2.0中的System.Collections namespace多了一個1.1所沒有的成員 – System.Collections.Generic
這個namespace最常用的應該是System.Collections.Generic.List這個class(因為我常用:P)
而這個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
...