Reputation: 13122
The code base where I work has code similar to the following in a few places:
Dim i As Integer
Dim ints As New ArrayList
ints.Add(1)
'... lets say we add more Integers
For each i in ints.ToArray(GetType(Integer))
'Do something
Next
I'm wondering what benefit is gained from the .ToArray(GetType(Integer))
since omitting that has nearly the same result at run-time. That is calling For Each i In ints
. The only difference I see is that if some type other than Integer is in the ArrayList
the resulting error message is more helpful if you do not call .ToArray()
Why would I want to use ToArray(type)
on an ArrayList
if I will be doing a For Each
loop where the type is already specified?
Upvotes: 3
Views: 667
Reputation: 26424
In your case, change to this code and you don't need to:
Dim i As Integer
Dim ints As New Generic.List(Of Integer)
ints.Add(1)
'... lets say we add more Integers
For each i in ints
'Do something
Next
ToArray
comes handy when dealing with LINQ results. If your collection size is measurable, you can do ToArray
. Most older .NET functions prefer to use arrays. Good example is String.Join
. So you may need to use it often for type compatibility between calls. Another reason to do so is that arrays are easier to inspect in debugger.
In any case, you should always justify the need. If your goal is strict typing, better use types that were created for this purpose, such as the above Generic.List(Of T)
.
Upvotes: 1
Reputation: 3908
Defensive programming in an old code base. Presumably they didn't have "List(Of Integer)" available at the time. There are a couple reasons you want to fail at the "For Each" statement (if "ints" contains a non-Integer) rather than later on in the loop:
Upvotes: 1
Reputation: 415810
There are two reasons you might call .ToArray()
on that ArrayList:
Upvotes: 1
Reputation: 726599
You do not need to call ToArray
on an ArrayList
in a For each
loop: i
is already declared as Integer
, so the type conversion will be performed for you automatically.
Sometimes you may need to call an array on results of a query to copy the data into memory. One place where it gives you advantages is copying results of a database query into memory, because you can close the connection sooner. In addition, calling ToList
could help you in situations when a query would otherwise reference a modified variable, or when IEnumerable
is expensive to produce multiple times.
Upvotes: 2
Reputation: 564433
Why would I want to use ToArray(type) on an ArrayList if I will be doing a For Each loop where the type is already specified?
In general, you likely don't want to do this. Calling ToArray()
actually creates a new array of your values.
The one place where this could be beneficial is if your loop is going to modify the ArrayList
. You can't modify most collections while enumerating them, so you'd get an exception. By adding the ToArray
call, you are enumerating a copy of the collection (as an array), so you can modify the original.
Note that, in general, if that's the case, I'd use ToList()
, and not ToArray()
, as it's typically a bit more efficient (often one fewer memory allocation). I'd also recommend using List(Of Integer)
instead of ArrayList
for any new code, as you get type safety.
Upvotes: 5