Skip to content

Repo sync for protected CLA branch #4609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions docs/dotnet/for-each-in.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
---
title: "for each, in"
description: "C++/CLI for each, in statement description and examples."
ms.date: 04/15/2022
description: "C++/CLI for each, in, statement descriptions and examples."
ms.date: 06/29/2023
ms.topic: "reference"
f1_keywords: ["cliext::foreach", "each_CPP", "in_CPP", "for each_CPP", "for each", "in"]
helpviewer_keywords: ["for each keyword [C++]"]
ms.assetid: 0c3a364b-2747-43f3-bb8d-b7d3b7023f79
---
# `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 isn't recommended. Consider using a standard [Range-based for Statement (C++)](../cpp/range-based-for-statement-cpp.md) instead.
Iterates through an array or collection. This nonstandard keyword is available in both C++/CLI and native C++ projects. However, using a standard [Range-based for Statement (C++)](../cpp/range-based-for-statement-cpp.md) is preferred, instead.

## All Runtimes
## All runtimes

### Syntax

Expand Down Expand Up @@ -57,29 +56,34 @@ This example shows how to use `for each` to iterate through a string.
#include <stdio.h>
using namespace Platform;

ref struct MyClass {
ref struct MyClass
{
property String^ MyStringProperty;
};

int main() {
int main()
{
String^ MyString = ref new String("abcd");

for each ( char c in MyString )
{
wprintf("%c", c);
}

wprintf("/n");
wprintf("\n");

MyClass^ x = ref new MyClass();
x->MyStringProperty = "Testing";

for each( char c in x->MyStringProperty )
{
wprintf("%c", c);
}
}
```

```Output
abcd

Testing
```

Expand Down Expand Up @@ -107,29 +111,34 @@ This example shows how to use `for each` to iterate through a string.
// compile with: /clr
using namespace System;

ref struct MyClass {
ref struct MyClass
{
property String ^ MyStringProperty;
};

int main() {
int main()
{
String ^ MyString = gcnew String("abcd");

for each ( Char c in MyString )
{
Console::Write(c);
}

Console::WriteLine();

MyClass ^ x = gcnew MyClass();
MyClass ^x = gcnew MyClass();
x->MyStringProperty = "Testing";

for each( Char c in x->MyStringProperty )
{
Console::Write(c);
}
}
```

```Output
abcd

Testing
```

Expand Down