The For Each statement enumerates the elements of a collection, executing the embedded statement block for each collection element.
foreach-statement:
For Each identifier In expression Do statement-listopt End For
Each For Each statement includes the identifier of the variable named iteration variable. During the statement execution the iteration variable represents a collection element, for which iteration is currently being executed. A compile error occurs if the embedded statement attempts to modify the iteration variable or pass the iteration variable as a reference parameter.
The expression in the statement (its type) must be a collection, and an explicit conversion must exist from the element type of the collection to the type of the iteration variable. If the expression value in the statement is Null, System.NullReferenceException is raised.
A type C is said to be a collection type if it implements the System.Collections.IEnumerable interface or implements the collection pattern by meeting all of the following criteria:
A type C contains the public instance method GetEnumerator that has no parameters and returns a structure-type, class-type, or interface-type value, which is named E in the following text.
A type E contains the public instance method MoveNext that has no parameters and returns a Boolean-type value.
A type E contains the public instance property Current that permits reading the current value. The type of this property is named the element type of the collection.
The System.Array system type is also a collection type. Since all array types derive from this type, any array type expression is permitted in the For Each statement. The order, in which array elements are traversed, is as follows. For single-dimensional arrays, elements are traversed in increasing index order from the least to the greatest. For multi-dimensional arrays, elements are traversed such that the indices of the rightmost dimension are increased first, then the next left dimension, and the leftmost dimension indices are increased last.
The For Each statement is in the following form:
For Each element In collection Do statement End For
corresponds to one of two possible expansions:
If the collection expression is of a type that implements the collection pattern (as declared above), the expansion of the For Each statement is:
Var
enumerator: E;
element: ElementType
disposable: IDisposable;
…
enumerator := (collection).GetEnumerator;
Try
While enumerator.MoveNext Do
element := enumerator.Current As ElementType;
statement;
End While
Finally
disposable := enumerator as System.IDisposable;
If disposable <> Null
disposable.Dispose;
End If
End Try
Otherwise, if the expression is of a type implementing interface, it is expanded as follows:
Var
enumerator: IEnumerator;
element: ElementType
disposable: IDisposable;
…
enumerator := ((collection) As System.Collections.IEnumerable).GetEnumerator;
Try
While enumerator.MoveNext Do
element := enumerator.Current As ElementType;
statement;
End While
Finally
disposable := enumerator as System.IDisposable;
If disposable <> Null
disposable.Dispose;
End If
End Try
Private Sub TestForEach();
Var
s: Array Of string;
s1: string;
Begin
For Each s1 In s Do
//Operations with s1
End For;
End Sub;
See also: