for each, infor each, in

通过数组或集合迭代。Iterates through an array or collection. 此非标准关键字在 C++/CLI 和本机 C++ 项目中可用。This non-standard keyword is available in both C++/CLI and native C++ projects. 但是,建议不使用它。However, its use is not recommended. 请考虑使用标准基于范围的 for 语句 (C++)相反。Consider using a standard Range-based for Statement (C++) instead.

所有运行时All Runtimes

语法Syntax

每个 ( 类型 标识符 表达式 ) {for each ( type identifier in expression ) {
    语句    statements
}}

参数Parameters

typetype
identifier 的类型。The type of identifier.

identifieridentifier
代表集合元素的迭代变量。The iteration variable that represents the collection element. identifier跟踪引用运算符,可以修改该元素。When identifier is a Tracking Reference Operator, you can modify the element.

expressionexpression
数组表达式或集合。An array expression or collection. 集合元素必须让编译器能将其转换为 identifier 类型。The collection element must be such that the compiler can convert it to the identifier type.

statementsstatements
要执行的一个或多个语句。One or more statements to be executed.

备注Remarks

for each 语句用于循环访问集合。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. 为集合中的所有元素完成迭代后,控制将传递给 for each 块之后的语句。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 eachin上下文相关的关键字for each and in are context-sensitive keywords.

更多相关信息:For more information:

Windows 运行时Windows Runtime

要求Requirements

编译器选项: /ZWCompiler option: /ZW

示例Example

本示例演示了如何使用 for each 来循环访问字符串。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

CLR 语法等同于所有运行时语法中,按如下所示除外。The CLR syntax is the same as the All Runtimes syntax, except as follows.

expressionexpression
托管的数组表达式或集合。A managed array expression or collection. 集合元素必须让编译器可以将其从转换Object标识符类型。The collection element must be such that the compiler can convert it from Object to the identifier type.

表达式计算结果为的类型实现IEnumerableIEnumerable<T>,或定义的类型GetEnumerator方法可返回一种类型的实现IEnumerator声明所有中定义的方法或IEnumerator.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

编译器选项: /clrCompiler option: /clr

示例Example

本示例演示了如何使用 for each 来循环访问字符串。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