Fredrik,
You forget one important factor also, FOR EACH takes alot of memory
(depending on your array size)
FOR EACH does not use a lot of memory! Normally an Enumerator needs to store
2 pieces of information, the current position & the 'list' it is
enumerating.
Irregardless of the size of the arraylist, the size of a reference to that
ArrayList & the size of the current index into the array list is going to
remain constant.
When you call ArrayList.GetEnumerator normally get a
System.Collections.ArrayList.ArrayListEnumeratorSimple returned.
If you use ISDASM.EXE on mscorlib.dll (the assembly where ArrayList is) you
will find that the above class contains 4 fields.
- currentElement
- index
- list
- version
Even ArrayListEnumerator only has 2 more fields (startIndex & endIndex).
Which definitely is not alot of memory!
Speed wise For Next may well be faster, however I normally use For Each to
be consistent across all collection types. Remember some collections do not
support indexing by integers. Also indexing by an integer may actually be an
expensive operation based on the type of collection (a true Linked List for
example). What may be true for Array & ArrayList will not necessarily be
true for all collections.
Hope this helps
Jay