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 isn't 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.

識別碼identifier
表示集合項目的反覆項目變數。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.

語句statements
要執行的一個或多個陳述式。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 can't 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.

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);
}
abcd

Testing

Common Language RuntimeCommon Language Runtime

備註Remarks

CLR 語法與 所有運行 時間語法相同,不同之處如下。The CLR syntax is the same as the All Runtimes syntax, except as follows.

expressionexpression
Managed 陣列運算式或集合。A managed array expression or collection. Collection 元素必須如此,編譯器才能將它從轉換成 Object 識別碼 型別。The collection element must be such that the compiler can convert it from Object to the identifier type.

運算式 會評估為型別,這個型別會定義方法,這個型別會傳回型別,而該型別會傳回實 IEnumerable IEnumerable<T> 或宣告 GetEnumerator IEnumerator 中定義的所有方法 IEnumeratorexpression 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);
}
abcd

Testing

另請參閱See also

執行時間平臺的元件擴充功能Component Extensions for Runtime Platforms
以範圍為基礎的 for 語句 (c + +) Range-based for statement (C++)