我一定会放弃使用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