for each, in
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 (type identifier in expression) {
statements
}
Parameters
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:
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.
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