title | ms.custom | ms.date | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
for each, in | Microsoft Docs |
11/04/2016 |
|
reference |
|
|
|
0c3a364b-2747-43f3-bb8d-b7d3b7023f79 |
24 |
mikeblome |
mblome |
ghogen |
Iterates through an array or collection. This non-standard keyword is available in both C++/CLI and native C++ projects. However, its use is not recommended. Consider using a standard Range-based for Statement (C++) instead.
Syntax
for each (typeidentifierinexpression) {
statements
}
Parameters
type
The type of identifier
.
identifier
The iteration variable that represents the collection element. When identifier
is a Tracking Reference Operator, you can modify the element.
expression
An array expression or collection. The collection element must be such that the compiler can convert it to the identifier
type.
statements
One or more statements to be executed.
Remarks
The for each
statement is used to iterate through a collection. You can modify elements in a collection, but you cannot add or delete elements.
The statements are executed for each element in the array or collection. After the iteration has been completed for all the elements in the collection, control is transferred to the statement that follows the for each
block.
for each
and in
are context-sensitive keywords.
For more information:
-
Iterating Over C++ Standard Library Collection By Using for each
-
How to: Iterate Over a User-Defined Collection with for each
Compiler option: /ZW
This example shows how to use for each
to iterate through a string.
// for_each_string1.cpp
// compile with: /ZW
#include <stdio.h>
using namespace Platform;
ref struct MyClass {
property String^ MyStringProperty;
};
int main() {
String^ MyString = ref new String("abcd");
for each ( char c in MyString )
wprintf("%c", c);
wprintf("/n");
MyClass^ x = ref new MyClass();
x->MyStringProperty = "Testing";
for each( char c in x->MyStringProperty )
wprintf("%c", c);
}
Output
abcd
Testing
Remarks
The CLR syntax is the same as the All Runtimes syntax, except as follows.
expression
A managed array expression or collection. The collection element must be such that the compiler can convert it from xref:System.Object to the identifier type.
expression evaluates to a type that implements xref:System.Collections.IEnumerable, xref:System.Collections.Generic.IEnumerable%601, or a type that defines a GetEnumerator
method that either returns a type that implements xref:System.Collections.IEnumerator or declares all of the methods that are defined in IEnumerator
.
Compiler option: /clr
This example shows how to use for each
to iterate through a string.
// for_each_string2.cpp
// compile with: /clr
using namespace System;
ref struct MyClass {
property String ^ MyStringProperty;
};
int main() {
String ^ MyString = gcnew String("abcd");
for each ( Char c in MyString )
Console::Write(c);
Console::WriteLine();
MyClass ^ x = gcnew MyClass();
x->MyStringProperty = "Testing";
for each( Char c in x->MyStringProperty )
Console::Write(c);
}
Output
abcd
Testing