Partager via


for each, in

 

The latest version of this topic can be found at 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.

All Runtimes

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:

Windows Runtime

Requirements

Compiler option: /ZW

Example

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  

Common Language Runtime

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 Object to the identifier type.

expression evaluates to a type that implements IEnumerable, IEnumerable<T>, or a type that defines a GetEnumerator method that either returns a type that implements IEnumerator or declares all of the methods that are defined in IEnumerator.

Requirements

Compiler option: /clr

Example

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   

See Also

Component Extensions for Runtime Platforms