PART I
C h a p t e r 7 :
A r r a y s a n d S t r i n g s
153
PART IPART I
foreach(
type loopvar
in
collection
)
statement
;
Here,
type loopvar
specifies the type and name of an
iteration variable.
The iteration variable
receives the value of the next element in the collection each time the
foreach
loop iterates.
The collection being cycled through is specified by
collection,
which, for the rest of this
discussion, is an array. Thus,
type
must be the same as (or compatible with) the element
type of the array. Beginning with C# 3.0,
type
can also be
var
, in which case the compiler
determines the type based on the element type of the array. This can be useful when
working with certain queries, as described later in this book. Normally, you will explicitly
specify the type.
Here is how
foreach
works. When the loop begins, the first element in the array is
obtained and assigned to
loopvar.
Each subsequent iteration obtains the next element from
the array and stores it in
loopvar.
The loop ends when there are no more elements to obtain.
Thus, the
Do'stlaringiz bilan baham: |