|
1 | 1 | ---
|
2 | 2 | title: "for each, in"
|
3 |
| -description: "C++/CLI for each, in statement description and examples." |
4 |
| -ms.date: 04/15/2022 |
| 3 | +description: "C++/CLI for each, in, statement descriptions and examples." |
| 4 | +ms.date: 06/29/2023 |
5 | 5 | ms.topic: "reference"
|
6 | 6 | f1_keywords: ["cliext::foreach", "each_CPP", "in_CPP", "for each_CPP", "for each", "in"]
|
7 | 7 | helpviewer_keywords: ["for each keyword [C++]"]
|
8 |
| -ms.assetid: 0c3a364b-2747-43f3-bb8d-b7d3b7023f79 |
9 | 8 | ---
|
10 | 9 | # `for each`, `in`
|
11 | 10 |
|
12 |
| -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. |
| 11 | +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. |
13 | 12 |
|
14 |
| -## All Runtimes |
| 13 | +## All runtimes |
15 | 14 |
|
16 | 15 | ### Syntax
|
17 | 16 |
|
@@ -57,29 +56,34 @@ This example shows how to use `for each` to iterate through a string.
|
57 | 56 | #include <stdio.h>
|
58 | 57 | using namespace Platform;
|
59 | 58 |
|
60 |
| -ref struct MyClass { |
| 59 | +ref struct MyClass |
| 60 | +{ |
61 | 61 | property String^ MyStringProperty;
|
62 | 62 | };
|
63 | 63 |
|
64 |
| -int main() { |
| 64 | +int main() |
| 65 | +{ |
65 | 66 | String^ MyString = ref new String("abcd");
|
66 | 67 |
|
67 | 68 | for each ( char c in MyString )
|
| 69 | + { |
68 | 70 | wprintf("%c", c);
|
| 71 | + } |
69 | 72 |
|
70 |
| - wprintf("/n"); |
| 73 | + wprintf("\n"); |
71 | 74 |
|
72 | 75 | MyClass^ x = ref new MyClass();
|
73 | 76 | x->MyStringProperty = "Testing";
|
74 | 77 |
|
75 | 78 | for each( char c in x->MyStringProperty )
|
| 79 | + { |
76 | 80 | wprintf("%c", c);
|
| 81 | + } |
77 | 82 | }
|
78 | 83 | ```
|
79 | 84 |
|
80 | 85 | ```Output
|
81 | 86 | abcd
|
82 |
| -
|
83 | 87 | Testing
|
84 | 88 | ```
|
85 | 89 |
|
@@ -107,29 +111,34 @@ This example shows how to use `for each` to iterate through a string.
|
107 | 111 | // compile with: /clr
|
108 | 112 | using namespace System;
|
109 | 113 |
|
110 |
| -ref struct MyClass { |
| 114 | +ref struct MyClass |
| 115 | +{ |
111 | 116 | property String ^ MyStringProperty;
|
112 | 117 | };
|
113 | 118 |
|
114 |
| -int main() { |
| 119 | +int main() |
| 120 | +{ |
115 | 121 | String ^ MyString = gcnew String("abcd");
|
116 | 122 |
|
117 | 123 | for each ( Char c in MyString )
|
| 124 | + { |
118 | 125 | Console::Write(c);
|
| 126 | + } |
119 | 127 |
|
120 | 128 | Console::WriteLine();
|
121 | 129 |
|
122 |
| - MyClass ^ x = gcnew MyClass(); |
| 130 | + MyClass ^x = gcnew MyClass(); |
123 | 131 | x->MyStringProperty = "Testing";
|
124 | 132 |
|
125 | 133 | for each( Char c in x->MyStringProperty )
|
| 134 | + { |
126 | 135 | Console::Write(c);
|
| 136 | + } |
127 | 137 | }
|
128 | 138 | ```
|
129 | 139 |
|
130 | 140 | ```Output
|
131 | 141 | abcd
|
132 |
| -
|
133 | 142 | Testing
|
134 | 143 | ```
|
135 | 144 |
|
|
0 commit comments