X hits on this document

781 views

0 shares

0 downloads

0 comments

188 / 396

Chapter ‎18   Unsafe code

If the for-condition is not present or if the evaluation yields true, control is transferred to the embedded statement. When and if control reaches the end point of the embedded statement (possibly from execution of a continue statement), the expressions of the for-iterator, if any, are evaluated in sequence, and then another iteration is performed, starting with evaluation of the for-condition in the step above.

If the for-condition is present and the evaluation yields false, control is transferred to the end point of the for statement.

Within the embedded statement of a for statement, a break statement (§‎8.9.1) may be used to transfer control to the end point of the for statement (thus ending iteration of the embedded statement), and a continue statement (§‎8.9.2) may be used to transfer control to the end point of the embedded statement (thus executing the for-iterator and performing another iteration of the for statement, starting with the for-condition).

The embedded statement of a for statement is reachable if one of the following is true:

The for statement is reachable and no for-condition is present.

The for statement is reachable and a for-condition is present and does not have the constant value false.

The end point of a for statement is reachable if at least one of the following is true:

The for statement contains a reachable break statement that exits the for statement.

The for statement is reachable and a for-condition is present and does not have the constant value true.

8.8.4 The foreach statement

The foreach statement enumerates the elements of a collection, executing an embedded statement for each element of the collection.

foreach-statement: foreach   (   type   identifier   in   expression   )   embedded-statement

The type and identifier of a foreach statement declare the iteration variable of the statement. The iteration variable corresponds to a read-only local variable with a scope that extends over the embedded statement. During execution of a foreach statement, the iteration variable represents the collection element for which an iteration is currently being performed. A compile-time error occurs if the embedded statement attempts to modify the iteration variable (via assignment or the ++ and ‑‑ operators) or pass the iteration variable as a ref or out parameter.

The type of the expression of a foreach statement must be a collection type (as defined below), and an explicit conversion (§‎6.2) must exist from the element type of the collection to the type of the iteration variable. If expression has the value null, a System.NullReferenceException is thrown.

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:

C contains a public instance method with the signature GetEnumerator() that returns a struct-type, class-type, or interface-type, which is called E in the following text.

E contains a public instance method with the signature MoveNext() and the return type bool.

E contains a public instance property named Current that permits reading the current value. The type of this property is said to be the element type of the collection type.

A type that implements IEnumerable is also a collection type, even if it doesn't satisfy the conditions above. (This is possible if it implements some of the IEnumerable members via explicit interface member implementation, as described in §‎13.4.1.)

The System.Array type (§‎12.1.1) is a collection type, and since all array types derive from System.Array, any array type expression is permitted in a foreach statement. The order in which foreach traverses the elements of

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.175

Document info
Document views781
Page views781
Page last viewedSat Dec 03 11:53:29 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments