Skip to content

Commit 54231aa

Browse files
authored
Merge pull request #5617 from dotnet/master
Update Live with current Master
2 parents b7763f3 + 1aa1f3c commit 54231aa

File tree

27 files changed

+151
-402
lines changed

27 files changed

+151
-402
lines changed

.openpublishing.redirection.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,10 @@
12971297
{
12981298
"source_path":"docs/csharp/language-reference/operators/null-conditional-operator.md",
12991299
"redirect_url":"/dotnet/csharp/language-reference/operators/null-coalescing-operator"
1300+
},
1301+
{
1302+
"source_path":"docs/csharp/programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md",
1303+
"redirect_url":"/dotnet/csharp/language-reference/keywords/foreach-in"
13001304
}
13011305
]
13021306
}

docs/csharp/how-to/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ You create classes and structs to implement your program. These techniques are c
4242
These articles help you work with collections of data.
4343

4444
- [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).
4645

47-
## strings
46+
## Working with strings
4847

4948
Strings are the fundamental data type used to display or manipulate text. These articles demonstrate common practices with strings.
5049

docs/csharp/language-reference/compiler-messages/cs1579.md

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
---
22
title: "Compiler Error CS1579"
3-
ms.date: 07/20/2015
3+
ms.date: 05/24/2018
44
f1_keywords:
55
- "CS1579"
66
helpviewer_keywords:
77
- "CS1579"
88
ms.assetid: 1eba84ce-58df-4fe3-9134-e26efefdc4ab
99
---
1010
# 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:
21+
2822
```csharp
2923
// CS1579.cs
3024
using System;
@@ -59,16 +53,10 @@ public class MyCollection
5953
public bool MoveNext()
6054
{
6155
nIndex++;
62-
return(nIndex < collection.items.GetLength(0));
56+
return (nIndex < collection.items.Length);
6357
}
6458

65-
public int Current
66-
{
67-
get
68-
{
69-
return(collection.items[nIndex]);
70-
}
71-
}
59+
public int Current => collection.items[nIndex];
7260
}
7361

7462
public static void Main()

docs/csharp/language-reference/keywords/codesnippet/CSharp/foreach-in_1.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "foreach, in (C# Reference)"
3-
ms.date: 10/11/2017
3+
ms.date: 05/24/2018
44
f1_keywords:
55
- "foreach"
66
- "foreach_CSharpKeyword"
@@ -12,55 +12,35 @@ ms.assetid: 5a9c5ddc-5fd3-457a-9bb6-9abffcd874ec
1212
---
1313
# foreach, in (C# Reference)
1414

15-
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:
2016

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>.
2219

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.
2421

25-
[Using foreach with Arrays](../../programming-guide/arrays/using-foreach-with-arrays.md)
22+
## Examples
2623

27-
[How to: Access a Collection Class with foreach](../../programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md)
24+
[!INCLUDE[interactive-note](~/includes/csharp-interactive-note.md)]
2825

29-
## Example
26+
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:
3027

31-
The following code shows three examples:
28+
[!code-csharp-interactive[list example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#1)]
3229

33-
> [!TIP]
34-
> 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:
3731

38-
- a typical `foreach` loop that displays the contents of an array of integers
32+
[!code-csharp-interactive[span example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#2)]
3933

40-
[!code-csharp-interactive[csrefKeywordsIteration#4](./codesnippet/CSharp/foreach-in_1.cs#L12-L26)]
41-
42-
- a [for](../../../csharp/language-reference/keywords/for.md) loop that does the same thing
43-
44-
[!code-csharp-interactive[csrefKeywordsIteration#4](./codesnippet/CSharp/foreach-in_1.cs#L31-L46)]
45-
46-
- a `foreach` loop that maintains a count of the number of elements in the array
47-
48-
[!code-csharp-interactive[csrefKeywordsIteration#4](./codesnippet/CSharp/foreach-in_1.cs#L51-L69)]
49-
5034
## C# Language Specification
5135

5236
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
5337

54-
## See Also
55-
56-
[The foreach statement (C# language specification)](/dotnet/csharp/language-reference/language-specification/statements#the-foreach-statement)
57-
58-
[C# Reference](../index.md)
59-
60-
[C# Programming Guide](../../programming-guide/index.md)
61-
62-
[C# Keywords](index.md)
63-
64-
[Iteration Statements](iteration-statements.md)
38+
## See also
6539

66-
[for](for.md)
40+
[The foreach statement (C# language specification)](/dotnet/csharp/language-reference/language-specification/statements#the-foreach-statement)
41+
[Using foreach with Arrays](../../programming-guide/arrays/using-foreach-with-arrays.md)
42+
[for](for.md)
43+
[Iteration Statements](iteration-statements.md)
44+
[C# Keywords](index.md)
45+
[C# Reference](../index.md)
46+
[C# Programming Guide](../../programming-guide/index.md)

docs/csharp/language-reference/keywords/iteration-statements.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@ helpviewer_keywords:
77
ms.assetid: 7d494566-bf75-4ee8-979f-0f964209437e
88
---
99
# 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:
13-
14-
- [do](../../../csharp/language-reference/keywords/do.md)
15-
16-
- [for](../../../csharp/language-reference/keywords/for.md)
17-
18-
- [foreach](../../../csharp/language-reference/keywords/foreach-in.md)
19-
20-
- [in](../../../csharp/language-reference/keywords/foreach-in.md)
21-
22-
- [while](../../../csharp/language-reference/keywords/while.md)
23-
24-
## See Also
25-
[C# Reference](../../../csharp/language-reference/index.md)
26-
[C# Programming Guide](../../../csharp/programming-guide/index.md)
27-
[C# Keywords](../../../csharp/language-reference/keywords/index.md)
28-
[Statement Keywords](../../../csharp/language-reference/keywords/statement-keywords.md)
10+
11+
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:
14+
15+
- [do](do.md)
16+
17+
- [for](for.md)
18+
19+
- [foreach, in](foreach-in.md)
20+
21+
- [while](while.md)
22+
23+
## See also
24+
[C# Reference](../index.md)
25+
[C# Programming Guide](../../programming-guide/index.md)
26+
[C# Keywords](index.md)
27+
[Statement Keywords](statement-keywords.md)

docs/csharp/misc/cs0278.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ ms.assetid: 5418cbbe-bcec-4379-a6f6-410987beb96a
1010
# Compiler Warning (level 2) CS0278
1111
'type' does not implement the 'pattern name' pattern. 'method name' is ambiguous with 'method name'.
1212

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.
1414

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.
1816

1917
## Example
2018
The following sample generates CS0278.
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
---
22
title: "Using foreach with Arrays (C# Programming Guide)"
3-
ms.date: 07/20/2015
3+
ms.date: 05/23/2018
44
helpviewer_keywords:
55
- "arrays [C#], foreach"
66
- "foreach statement [C#], using with arrays"
77
ms.assetid: 5f2da2a9-1f56-4de5-94cc-e07f4f7a0244
88
---
99
# 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:
11-
12-
[!code-csharp[csProgGuideArrays#28](../../../csharp/programming-guide/arrays/codesnippet/CSharp/using-foreach-with-arrays_1.cs)]
13-
14-
With multidimensional arrays, you can use the same method to iterate through the elements, for example:
15-
16-
[!code-csharp[csProgGuideArrays#29](../../../csharp/programming-guide/arrays/codesnippet/CSharp/using-foreach-with-arrays_2.cs)]
17-
18-
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`:
14+
15+
[!code-csharp[csProgGuideArrays#28](./codesnippet/CSharp/using-foreach-with-arrays_1.cs)]
16+
17+
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:
18+
19+
[!code-csharp[csProgGuideArrays#29](./codesnippet/CSharp/using-foreach-with-arrays_2.cs)]
20+
21+
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.
22+
23+
## See also
2124
<xref:System.Array>
22-
[C# Programming Guide](../../../csharp/programming-guide/index.md)
23-
[Arrays](../../../csharp/programming-guide/arrays/index.md)
24-
[Single-Dimensional Arrays](../../../csharp/programming-guide/arrays/single-dimensional-arrays.md)
25-
[Multidimensional Arrays](../../../csharp/programming-guide/arrays/multidimensional-arrays.md)
26-
[Jagged Arrays](../../../csharp/programming-guide/arrays/jagged-arrays.md)
25+
[C# Programming Guide](../index.md)
26+
[Arrays](index.md)
27+
[Single-Dimensional Arrays](single-dimensional-arrays.md)
28+
[Multidimensional Arrays](multidimensional-arrays.md)
29+
[Jagged Arrays](jagged-arrays.md)

docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/how-to-access-a-collection-class-with-foreach_1.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)