Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,10 @@
{
"source_path":"docs/csharp/language-reference/operators/null-conditional-operator.md",
"redirect_url":"/dotnet/csharp/language-reference/operators/null-coalescing-operator"
},
{
"source_path":"docs/csharp/programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md",
"redirect_url":"/dotnet/csharp/language-reference/keywords/foreach-in"
}
]
}
3 changes: 1 addition & 2 deletions docs/csharp/how-to/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ You create classes and structs to implement your program. These techniques are c
These articles help you work with collections of data.

- [Initialize a dictionary with a collection initializer](../programming-guide/classes-and-structs/how-to-initialize-a-dictionary-with-a-collection-initializer.md).
- [Access all the elements in a collection using `foreach`](../programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md).

## strings
## Working with strings

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

Expand Down
40 changes: 14 additions & 26 deletions docs/csharp/language-reference/compiler-messages/cs1579.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
---
title: "Compiler Error CS1579"
ms.date: 07/20/2015
ms.date: 05/24/2018
f1_keywords:
- "CS1579"
helpviewer_keywords:
- "CS1579"
ms.assetid: 1eba84ce-58df-4fe3-9134-e26efefdc4ab
---
# Compiler Error CS1579
foreach statement cannot operate on variables of type 'type1' because 'type2' does not contain a public definition for 'identifier'

To iterate through a collection using the [foreach](../../../csharp/language-reference/keywords/foreach-in.md) statement, the collection must meet the following requirements:

- It must be an interface, class or struct.

- It must include a public <xref:System.Collections.IEnumerable.GetEnumerator%2A> method that returns a type.

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

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

## Example
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`.

The following sample generates CS1579.

foreach statement cannot operate on variables of type 'type1' because 'type2' does not contain a public definition for 'identifier'

To iterate through a collection using the [foreach](../keywords/foreach-in.md) statement, the collection must meet the following requirements:

- Its type must include a public parameterless `GetEnumerator` method whose return type is either class, struct, or interface type.
- 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>.

## Example

The following sample generates CS1579 because the `MyCollection` class doesn't contain the public `GetEnumerator` method:

```csharp
// CS1579.cs
using System;
Expand Down Expand Up @@ -59,16 +53,10 @@ public class MyCollection
public bool MoveNext()
{
nIndex++;
return(nIndex < collection.items.GetLength(0));
return (nIndex < collection.items.Length);
}

public int Current
{
get
{
return(collection.items[nIndex]);
}
}
public int Current => collection.items[nIndex];
}

public static void Main()
Expand Down

This file was deleted.

58 changes: 19 additions & 39 deletions docs/csharp/language-reference/keywords/foreach-in.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "foreach, in (C# Reference)"
ms.date: 10/11/2017
ms.date: 05/24/2018
f1_keywords:
- "foreach"
- "foreach_CSharpKeyword"
Expand All @@ -12,55 +12,35 @@ ms.assetid: 5a9c5ddc-5fd3-457a-9bb6-9abffcd874ec
---
# foreach, in (C# Reference)

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.

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.

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

A `foreach` loop can also be exited by the [goto](goto.md), [return](return.md), or [throw](throw.md) statements.
- has the public parameterless `GetEnumerator` method whose return type is either class, struct, or interface type,
- 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>.

For more information about the `foreach` keyword and code samples, see the following topics:
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.

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

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

## Example
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:

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

> [!TIP]
> You can modify the examples to experiment with the syntax and try different
> usages that are more similar to your use case. Press "Run" to run the code,
> then edit and press "Run" again.
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:

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

[!code-csharp-interactive[csrefKeywordsIteration#4](./codesnippet/CSharp/foreach-in_1.cs#L12-L26)]

- a [for](../../../csharp/language-reference/keywords/for.md) loop that does the same thing

[!code-csharp-interactive[csrefKeywordsIteration#4](./codesnippet/CSharp/foreach-in_1.cs#L31-L46)]

- a `foreach` loop that maintains a count of the number of elements in the array

[!code-csharp-interactive[csrefKeywordsIteration#4](./codesnippet/CSharp/foreach-in_1.cs#L51-L69)]

## C# Language Specification

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

## See Also

[The foreach statement (C# language specification)](/dotnet/csharp/language-reference/language-specification/statements#the-foreach-statement)

[C# Reference](../index.md)

[C# Programming Guide](../../programming-guide/index.md)

[C# Keywords](index.md)

[Iteration Statements](iteration-statements.md)
## See also

[for](for.md)
[The foreach statement (C# language specification)](/dotnet/csharp/language-reference/language-specification/statements#the-foreach-statement)
[Using foreach with Arrays](../../programming-guide/arrays/using-foreach-with-arrays.md)
[for](for.md)
[Iteration Statements](iteration-statements.md)
[C# Keywords](index.md)
[C# Reference](../index.md)
[C# Programming Guide](../../programming-guide/index.md)
37 changes: 18 additions & 19 deletions docs/csharp/language-reference/keywords/iteration-statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ helpviewer_keywords:
ms.assetid: 7d494566-bf75-4ee8-979f-0f964209437e
---
# Iteration Statements (C# Reference)
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.

The following keywords are used in iteration statements:

- [do](../../../csharp/language-reference/keywords/do.md)

- [for](../../../csharp/language-reference/keywords/for.md)

- [foreach](../../../csharp/language-reference/keywords/foreach-in.md)

- [in](../../../csharp/language-reference/keywords/foreach-in.md)

- [while](../../../csharp/language-reference/keywords/while.md)

## See Also
[C# Reference](../../../csharp/language-reference/index.md)
[C# Programming Guide](../../../csharp/programming-guide/index.md)
[C# Keywords](../../../csharp/language-reference/keywords/index.md)
[Statement Keywords](../../../csharp/language-reference/keywords/statement-keywords.md)

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.

The following keywords are used in iteration statements:

- [do](do.md)

- [for](for.md)

- [foreach, in](foreach-in.md)

- [while](while.md)

## See also
[C# Reference](../index.md)
[C# Programming Guide](../../programming-guide/index.md)
[C# Keywords](index.md)
[Statement Keywords](statement-keywords.md)
6 changes: 2 additions & 4 deletions docs/csharp/misc/cs0278.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ ms.assetid: 5418cbbe-bcec-4379-a6f6-410987beb96a
# Compiler Warning (level 2) CS0278
'type' does not implement the 'pattern name' pattern. 'method name' is ambiguous with 'method name'.

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

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.

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

## Example
The following sample generates CS0278.
Expand Down
37 changes: 20 additions & 17 deletions docs/csharp/programming-guide/arrays/using-foreach-with-arrays.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
---
title: "Using foreach with Arrays (C# Programming Guide)"
ms.date: 07/20/2015
ms.date: 05/23/2018
helpviewer_keywords:
- "arrays [C#], foreach"
- "foreach statement [C#], using with arrays"
ms.assetid: 5f2da2a9-1f56-4de5-94cc-e07f4f7a0244
---
# Using foreach with Arrays (C# Programming Guide)
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:

[!code-csharp[csProgGuideArrays#28](../../../csharp/programming-guide/arrays/codesnippet/CSharp/using-foreach-with-arrays_1.cs)]

With multidimensional arrays, you can use the same method to iterate through the elements, for example:

[!code-csharp[csProgGuideArrays#29](../../../csharp/programming-guide/arrays/codesnippet/CSharp/using-foreach-with-arrays_2.cs)]

However, with multidimensional arrays, using a nested [for](../../../csharp/language-reference/keywords/for.md) loop gives you more control over the array elements.

## See Also

The [foreach](../../language-reference/keywords/foreach-in.md) statement provides a simple, clean way to iterate through the elements of an array.

For single-dimensional arrays, the `foreach` statement processes elements in increasing index order, starting with index 0 and ending with index `Length - 1`:

[!code-csharp[csProgGuideArrays#28](./codesnippet/CSharp/using-foreach-with-arrays_1.cs)]

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:

[!code-csharp[csProgGuideArrays#29](./codesnippet/CSharp/using-foreach-with-arrays_2.cs)]

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.

## See also
<xref:System.Array>
[C# Programming Guide](../../../csharp/programming-guide/index.md)
[Arrays](../../../csharp/programming-guide/arrays/index.md)
[Single-Dimensional Arrays](../../../csharp/programming-guide/arrays/single-dimensional-arrays.md)
[Multidimensional Arrays](../../../csharp/programming-guide/arrays/multidimensional-arrays.md)
[Jagged Arrays](../../../csharp/programming-guide/arrays/jagged-arrays.md)
[C# Programming Guide](../index.md)
[Arrays](index.md)
[Single-Dimensional Arrays](single-dimensional-arrays.md)
[Multidimensional Arrays](multidimensional-arrays.md)
[Jagged Arrays](jagged-arrays.md)

This file was deleted.

Loading