开发者

Don't use ArrayList!

People often tell me not to use ArrayList for making my arrays in VB.NET. I would like to hear opinions about that,开发者_Python百科 why shouldn't I? What is the best method for creating and manipulating array contents, dimensions etc?

Thanks.


Use generic lists instead. ArrayList is not typed, meaning that you can have a list with strings, numbers, +++. Rather you should use a generic list like this:

Dim list1 As New List(Of String) ' This beeing a list of string

The lists-class also allows you to expand the list on the fly, however, it also enforces typing which helps write cleaner code (you don't have to typecast) and code that is less prone to bugs.

ArrayList is gennerally speaking just a List(Of Object).


ArrayLists are not type checked so you will need to do a lot of boxing/unboxing. Use a .net collection instead that support generics like List.

Because List does not have to unbox your objects it boasts a surprisingly better performance than Arraylist.


ArrayLists are less performant and memory-extensive:

Dim list1 As New ArrayList
For i As Integer = 1 To 100000000
    list1.Add(i)
Next
' --> OutOfMemoryException after 13.163 seconds, having added 67.108.864 items

Dim list2 As New List(Of Integer)
For i As Integer = 1 To 100000000
    list2.Add(i)
Next
' --> finished after 1.778 seconds, having added all values


Because its not strongly typed. Use a List(Of T) which T is your type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜