April 2018
Intermediate to advanced
300 pages
7h 41m
English
For and foreach are two of the alternative ways of iterating over a list of items. Each of them operates in a different way. The for loop actually loads all the items of the list in memory first and then uses an indexer to iterate over each element, whereas foreach uses an enumerator and iterates until it reaches the end of the list.
The following table shows the types of collections that are good to use for for and foreach:
|
Type |
For/Foreach |
|
Typed array |
Good for both |
|
Array list |
Better with for |
|
Generic collections |
Better with for |