我一定會放棄使用ArrayList
。爲什麼?因爲從.NET 2.0版開始引入泛型,這是很久以前的事情,強類型List(Of T)
已經在大多數實際應用中佔據了自己的位置。
即使你的情況,我會建議對一個ArrayList
,因爲你可以有一個更好,也更容易,例如一個訪問的數據結構—如下所示:
Structure CompositeValue ' or declare this as Class instead; see comment below!
Public Property SomeString() As String
Public Property FirstInteger() As Integer
Public Property SecondInteger() As Integer
Public Property MaybeFirstBoolean() As Nullable(Of Boolean)
Public Property MaybeSecondBoolean() As Nullable(Of Boolean)
End Structure
' Note that the above is the syntax for auto properties, which were introduced
' with VB.NET 10/VS 2010.
優點:這方式,你不會記得例如在ArrayList
的哪個位置有字符串或第二個整數值;編譯器將能夠確保您訪問您認爲正在訪問的屬性。此外,您的代碼將更容易閱讀和理解。
開始。NET 4,你也可以使用一個Tuple
(Of String, Integer, Integer, Nullable(Of Boolean), Nullable(Of Boolean))
:
Dim myCompositeValue = Tuple.Create("Foo", 1, 2, True, False)
但是,讓我說,元組不具有所有這些結構的優點,如上面的類結構;你會得到類型安全,但你不能爲這五個屬性給出明智的名稱。 VB.NET中的元組只不過是一組類型安全的匿名值。
異構集合通常只是一個愚蠢的想法,即使在動態語言。使用一個結構(或者,也許是類)。數組,ArrayLists,列表等都是存儲同一事物的0- *實例的集合,沒有別的。性能應該是你最不關心的問題。 – delnan 2011-01-30 22:45:34