Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C++
Reference
C/C++ Languages
for each, in
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual C++ Language Reference
for each, in

The for each statement is used to iterate through a collection of elements.

for each (type identifier in expression) {
      statements
}

Parameters

type

The type of identifier.

identifier

The iteration variable that represents the collection element. When identifier is a tracking reference, you can modify the element.

expression

A managed array expression or collection. The compiler must be able to convert the collection element from Object to the identifier type.

expression evaluates to a type that implements IEnumerable, IEnumerable, or a type that defines a GetEnumerator method. In the latter case, GetEnumerator should either return a type that implements IEnumerator or declares all the methods defined in IEnumerator.

statements

One or more statements to be executed.

The for each statement is used to iterate through a collection. It is possible to 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 next statement following the for each block.

for each and in are context-sensitive keywords; see Context-Sensitive Keywords for more information.

In the development environment, you can get F1 help on by highlighting the keyword, (for each) and pressing F1.

For more information, see,

This sample shows how to iterate through a string with for each.

// for_each_string.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

Compiler option: /clr

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
for each returns read only objects      John C. Lieurance   |   Edit   |   Show History

There is an error under the section Remarks in paragraph 1.

It is not possible to update the elements in a collection by updating the element that is returned by the for each command. The for each command returns an object by value, not by reference.

The for each command reference explains this:
http://msdn.microsoft.com/en-us/library/ttw7t8t6(VS.80).aspx

The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker