You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/how-to/index.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,9 +42,8 @@ You create classes and structs to implement your program. These techniques are c
42
42
These articles help you work with collections of data.
43
43
44
44
-[Initialize a dictionary with a collection initializer](../programming-guide/classes-and-structs/how-to-initialize-a-dictionary-with-a-collection-initializer.md).
45
-
-[Access all the elements in a collection using `foreach`](../programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md).
46
45
47
-
## strings
46
+
## Working with strings
48
47
49
48
Strings are the fundamental data type used to display or manipulate text. These articles demonstrate common practices with strings.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/cs1579.md
+14-26Lines changed: 14 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,24 @@
1
1
---
2
2
title: "Compiler Error CS1579"
3
-
ms.date: 07/20/2015
3
+
ms.date: 05/24/2018
4
4
f1_keywords:
5
5
- "CS1579"
6
6
helpviewer_keywords:
7
7
- "CS1579"
8
8
ms.assetid: 1eba84ce-58df-4fe3-9134-e26efefdc4ab
9
9
---
10
10
# Compiler Error CS1579
11
-
foreach statement cannot operate on variables of type 'type1' because 'type2' does not contain a public definition for 'identifier'
12
-
13
-
To iterate through a collection using the [foreach](../../../csharp/language-reference/keywords/foreach-in.md) statement, the collection must meet the following requirements:
14
-
15
-
- It must be an interface, class or struct.
16
-
17
-
- It must include a public <xref:System.Collections.IEnumerable.GetEnumerator%2A> method that returns a type.
18
-
19
-
- The return type must contain a public property named <xref:System.Collections.IEnumerator.Current%2A>, and a public method named <xref:System.Collections.IEnumerator.MoveNext%2A>.
20
-
21
-
- For more information, see [How to: Access a Collection Class with foreach](../../../csharp/programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md).
22
-
23
-
## Example
24
-
In this sample, [foreach](../../../csharp/language-reference/keywords/foreach-in.md) is not able to iterate through the collection because there is no `public`<xref:System.Collections.IEnumerable.GetEnumerator%2A> method in `MyCollection`.
25
-
26
-
The following sample generates CS1579.
27
-
11
+
foreach statement cannot operate on variables of type 'type1' because 'type2' does not contain a public definition for 'identifier'
12
+
13
+
To iterate through a collection using the [foreach](../keywords/foreach-in.md) statement, the collection must meet the following requirements:
14
+
15
+
- Its type must include a public parameterless `GetEnumerator` method whose return type is either class, struct, or interface type.
16
+
- The return type of the `GetEnumerator` method must contain a public property named `Current` and a public parameterless method named `MoveNext` whose return type is <xref:System.Boolean>.
17
+
18
+
## Example
19
+
20
+
The following sample generates CS1579 because the `MyCollection` class doesn't contain the public `GetEnumerator` method:
The `foreach` statement repeats a group of embedded statements for each element in an array or an object collection that implements the <xref:System.Collections.IEnumerable?displayProperty=nameWithType> or <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> interface. The [foreach statement](/dotnet/csharp/language-reference/language-specification/statements#the-foreach-statement) is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a [for](for.md) loop.
16
-
17
-
The embedded statements continue to execute 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 `foreach` block.
18
-
19
-
At any point within the `foreach` block, you can break out of the loop by using the [break](break.md) keyword, or step to the next iteration in the loop by using the [continue](continue.md) keyword.
15
+
The `foreach` statement executes a statement or a block of statements for each element in an instance of the type that implements the <xref:System.Collections.IEnumerable?displayProperty=nameWithType> or <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> interface. The `foreach` statement is not limited to those types and can be applied to an instance of any type that satisfies the following conditions:
20
16
21
-
A `foreach` loop can also be exited by the [goto](goto.md), [return](return.md), or [throw](throw.md) statements.
17
+
- has the public parameterless `GetEnumerator` method whose return type is either class, struct, or interface type,
18
+
- the return type of the `GetEnumerator` method has the public `Current` property and the public parameterless `MoveNext` method whose return type is <xref:System.Boolean>.
22
19
23
-
For more information about the `foreach`keyword and code samples, see the following topics:
20
+
At any point within the `foreach`statement block, you can break out of the loop by using the [break](break.md) keyword, or step to the next iteration in the loop by using the [continue](continue.md) keyword. You also can exit a `foreach` loop by the [goto](goto.md), [return](return.md), or [throw](throw.md) statements.
24
21
25
-
[Using foreach with Arrays](../../programming-guide/arrays/using-foreach-with-arrays.md)
22
+
## Examples
26
23
27
-
[How to: Access a Collection Class with foreach](../../programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md)
The following example shows usage of the `foreach` statement with an instance of the <xref:System.Collections.Generic.List%601> type that implements the <xref:System.Collections.Generic.IEnumerable%601> interface:
> You can modify the examples to experiment with the syntax and try different
35
-
> usages that are more similar to your use case. Press "Run" to run the code,
36
-
> then edit and press "Run" again.
30
+
The next example uses the `foreach` statement with an instance of the <xref:System.Span%601?displayProperty=nameWithType> type, which doesn't implement any interfaces:
37
31
38
-
- a typical `foreach` loop that displays the contents of an array of integers
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/keywords/iteration-statements.md
+18-19Lines changed: 18 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,22 +7,21 @@ helpviewer_keywords:
7
7
ms.assetid: 7d494566-bf75-4ee8-979f-0f964209437e
8
8
---
9
9
# Iteration Statements (C# Reference)
10
-
You can create loops by using the iteration statements. Iteration statements cause embedded statements to be executed a number of times, subject to the loop-termination criteria. These statements are executed in order, except when a [jump statement](../../../csharp/language-reference/keywords/jump-statements.md) is encountered.
11
-
12
-
The following keywords are used in iteration statements:
You can create loops by using the iteration statements. Iteration statements cause embedded statements to be executed a number of times, subject to the loop-termination criteria. These statements are executed in order, except when a [jump statement](../../../csharp/language-reference/keywords/jump-statements.md) is encountered.
12
+
13
+
The following keywords are used in iteration statements:
'type' does not implement the 'pattern name' pattern. 'method name' is ambiguous with 'method name'.
12
12
13
-
There are several statements in C# that rely on defined patterns, such as `foreach` and `using`. For example, `foreach` relies on the collection class implementing the "enumerable" pattern.
13
+
There are several statements in C# that rely on defined patterns, such as `foreach` and `using`. For example, the [`foreach`statement](../language-reference/keywords/foreach-in.md)relies on the collection class implementing the "enumerable" pattern.
14
14
15
-
CS0278 can occur if the compiler is unable to make the match due to ambiguities. For example, the "enumerable" pattern requires that there be a method called `MoveNext`, and your code might contain two methods called `MoveNext`. The compiler will attempt to find an interface to use, but it is recommended that you determine and resolve the cause of the ambiguity.
16
-
17
-
For more information, see [How to: Access a Collection Class with foreach](../../csharp/programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md).
15
+
CS0278 can occur if the compiler is unable to make the match due to ambiguities. For example, the "enumerable" pattern requires that there be a method called `MoveNext`, and your code might contain two methods called `MoveNext`. The compiler will attempt to find an interface to use, but it is recommended that you determine and resolve the cause of the ambiguity.
title: "Using foreach with Arrays (C# Programming Guide)"
3
-
ms.date: 07/20/2015
3
+
ms.date: 05/23/2018
4
4
helpviewer_keywords:
5
5
- "arrays [C#], foreach"
6
6
- "foreach statement [C#], using with arrays"
7
7
ms.assetid: 5f2da2a9-1f56-4de5-94cc-e07f4f7a0244
8
8
---
9
9
# Using foreach with Arrays (C# Programming Guide)
10
-
C# also provides the [foreach](../../../csharp/language-reference/keywords/foreach-in.md) statement. This statement provides a simple, clean way to iterate through the elements of an array or any enumerable collection. The `foreach` statement processes elements in the order returned by the array or collection type’s enumerator, which is usually from the 0th element to the last. For example, the following code creates an array called `numbers` and iterates through it with the `foreach` statement:
However, with multidimensional arrays, using a nested [for](../../../csharp/language-reference/keywords/for.md) loop gives you more control over the array elements.
19
-
20
-
## See Also
10
+
11
+
The [foreach](../../language-reference/keywords/foreach-in.md) statement provides a simple, clean way to iterate through the elements of an array.
12
+
13
+
For single-dimensional arrays, the `foreach` statement processes elements in increasing index order, starting with index 0 and ending with index `Length - 1`:
For multi-dimensional arrays, elements are traversed such that the indices of the rightmost dimension are increased first, then the next left dimension, and so on to the left:
However, with multidimensional arrays, using a nested [for](../../language-reference/keywords/for.md) loop gives you more control over the order in which to process the array elements.
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/how-to-access-a-collection-class-with-foreach_1.cs
0 commit comments