Skip to content

Commit 7c0cb6b

Browse files
authored
Merge pull request #5980 from dotnet/master
Update live with current master
2 parents 416988a + 3b5e31f commit 7c0cb6b

File tree

7 files changed

+103
-143
lines changed

7 files changed

+103
-143
lines changed

docs/csharp/language-reference/keywords/codesnippet/CSharp/for_1.cs

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

docs/csharp/language-reference/keywords/codesnippet/CSharp/for_2.cs

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

docs/csharp/language-reference/keywords/codesnippet/CSharp/for_3.cs

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 82 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "for (C# Reference)"
3-
ms.date: 07/20/2015
3+
ms.date: 06/13/2018
44
f1_keywords:
55
- "for"
66
- "for_CSharpKeyword"
@@ -10,98 +10,92 @@ ms.assetid: 34041a40-2c87-467a-9ffb-a0417d8f67a8
1010
---
1111
# for (C# reference)
1212

13-
By using a `for` loop, you can run a statement or a block of statements repeatedly until a specified expression evaluates to `false`. This kind of loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate.
14-
15-
## Example
13+
The `for` statement executes a statement or a block of statements while a specified boolean expression evaluates to `true`.
1614

17-
In the following example, the value of `i` is written to the console and incremented by 1 during each iteration of the loop:
18-
19-
[!code-csharp[csrefKeywordsIteration#2](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_1.cs)]
20-
21-
The [for statement](/dotnet/csharp/language-reference/language-specification/statements#the-for-statement) in the previous example performs the following actions:
22-
23-
1. First, the initial value of variable `i` is established. This step happens only once, regardless of how many times the loop repeats. You can think of this initialization as happening outside the looping process.
24-
25-
2. To evaluate the condition (`i <= 5`), the value of `i` is compared to 5.
26-
27-
- If `i` is less than or equal to 5, the condition evaluates to `true`, and the following actions occur.
28-
29-
1. The `Console.WriteLine` statement in the body of the loop displays the value of `i`.
30-
31-
2. The value of `i` is incremented by 1.
32-
33-
3. The loop returns to the start of step 2 to evaluate the condition again.
15+
At any point within the `for` statement block, you can break out of the loop by using the [break](break.md) statement, or step to the next iteration in the loop by using the [continue](continue.md) statement. You also can exit a `for` loop by the [goto](goto.md), [return](return.md), or [throw](throw.md) statements.
3416

35-
- If `i` is greater than 5, the condition evaluates to `false`, and you exit the loop.
36-
37-
Note that, if the initial value of `i` is greater than 5, the body of the loop doesn't run even once.
17+
## Structure of the `for` statement
3818

39-
## Sections of a for statement
40-
41-
Every [for statement](/dotnet/csharp/language-reference/language-specification/statements#the-for-statement) defines *initializer*, *condition*, and *iterator* sections. These sections usually determine how many times the loop iterates.
19+
The `for` statement defines *initializer*, *condition*, and *iterator* sections:
4220

43-
```csharp
21+
```csharp
4422
for (initializer; condition; iterator)
4523
body
46-
```
47-
48-
The sections serve the following purposes:
49-
50-
- The initializer section sets the initial conditions. The statements in this section run only once, before you enter the loop. The section can contain only one of the following two options.
51-
52-
- The declaration and initialization of a local loop variable, as the first example shows (`int i = 1`). The variable is local to the loop and can't be accessed from outside the loop.
53-
54-
- Zero or more statement expressons from the following list, separated by commas.
55-
56-
- [assignment](../../../csharp/language-reference/operators/assignment-operator.md) statement
57-
58-
- invocation of a method
59-
60-
- prefix or postfix [increment](../../../csharp/language-reference/operators/increment-operator.md) expression, such as `++i` or `i++`
61-
62-
- prefix or postfix [decrement](../../../csharp/language-reference/operators/decrement-operator.md) expression, such as `--i` or `i--`
63-
64-
- creation of an object by using [new](../../../csharp/language-reference/keywords/new-operator.md)
65-
66-
- [await](../../../csharp/language-reference/keywords/await.md) expression
67-
68-
- The condition section contains a boolean expression that’s evaluated to determine whether the loop should exit or should run again.
69-
70-
- The iterator section defines what happens after each iteration of the body of the loop. The iterator section contains zero or more of the following statement expressions, separated by commas:
71-
72-
- [assignment](../../../csharp/language-reference/operators/assignment-operator.md) statement
73-
74-
- invocation of a method
75-
76-
- prefix or postfix [increment](../../../csharp/language-reference/operators/increment-operator.md) expression, such as `++i` or `i++`
77-
78-
- prefix or postfix [decrement](../../../csharp/language-reference/operators/decrement-operator.md) expression, such as `--i` or `i--`
79-
80-
- creation of an object by using [new](../../../csharp/language-reference/keywords/new-operator.md)
81-
82-
- [await](../../../csharp/language-reference/keywords/await.md) expression
83-
84-
- The body of the loop consists of a statement, an empty statement, or a block of statements, which you create by enclosing zero or more statements in braces.
85-
86-
You can break out of a `for` loop by using the [break](../../../csharp/language-reference/keywords/break.md) keyword, or you can step to the next iteration by using the [continue](../../../csharp/language-reference/keywords/continue.md) keyword. You also can exit any loop by using a [goto](../../../csharp/language-reference/keywords/goto.md), [return](../../../csharp/language-reference/keywords/return.md), or [throw](../../../csharp/language-reference/keywords/throw.md) statement.
87-
88-
The first example in this topic shows the most typical kind of `for` loop, which makes the following choices for the sections:
89-
90-
- The initializer declares and initializes a local loop variable, `i`, that maintains a count of the iterations of the loop.
91-
92-
- The condition checks the value of the loop variable against a known final value, 5.
93-
94-
- The iterator section uses a postfix increment statement, `i++`, to tally each iteration of the loop.
24+
```
9525

96-
## More examples
97-
98-
The following example illustrates several less common choices: assigning a value to an external loop variable in the initializer section, invoking the `Console.WriteLine` method in both the initializer and the iterator sections, and changing the values of two variables in the iterator section.
26+
All three sections are optional. The body of the loop is either a statement or a block of statements.
27+
28+
The following example shows the `for` statement with all of the sections defined:
29+
30+
[!code-csharp-interactive[for loop example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#5)]
31+
32+
### The *initializer* section
33+
34+
The statements in the *initializer* section are executed only once, before entering the loop. The *initializer* section is either of the following:
35+
36+
- The declaration and initialization of a local loop variable, which can't be accessed from outside the loop.
37+
38+
- Zero or more statement expressions from the following list, separated by commas:
39+
40+
- [assignment](../operators/assignment-operator.md) statement
41+
42+
- invocation of a method
43+
44+
- prefix or postfix [increment](../operators/increment-operator.md) expression, such as `++i` or `i++`
45+
46+
- prefix or postfix [decrement](../operators/decrement-operator.md) expression, such as `--i` or `i--`
47+
48+
- creation of an object by using [new](new-operator.md) keyword
49+
50+
- [await](await.md) expression
51+
52+
The *initializer* section in the example above declares and initializes the local loop variable `i`:
53+
54+
```csharp
55+
int i = 0
56+
```
57+
58+
### The *condition* section
59+
60+
The *condition* section, if present, must be a boolean expression. That expression is evaluated before every loop iteration. If the *condition* section is not present or the boolean expression evaluates to `true`, the next loop iteration is executed; otherwise, the loop is exited.
61+
62+
The *condition* section in the example above determines if the loop terminates based on the value of the local loop variable:
63+
64+
```csharp
65+
i < 5
66+
```
67+
68+
### The *iterator* section
69+
70+
The *iterator* section defines what happens after each iteration of the body of the loop. The *iterator* section contains zero or more of the following statement expressions, separated by commas:
71+
72+
- [assignment](../operators/assignment-operator.md) statement
73+
74+
- invocation of a method
75+
76+
- prefix or postfix [increment](../operators/increment-operator.md) expression, such as `++i` or `i++`
77+
78+
- prefix or postfix [decrement](../operators/decrement-operator.md) expression, such as `--i` or `i--`
79+
80+
- creation of an object by using [new](new-operator.md) keyword
81+
82+
- [await](await.md) expression
83+
84+
The *iterator* section in the example above increments the local loop variable:
85+
86+
```csharp
87+
i++
88+
```
89+
90+
## Examples
91+
92+
The following example illustrates several less common usages of the `for` statement sections: assigning a value to an external loop variable in the *initializer* section, invoking a method in both the *initializer* and the *iterator* sections, and changing the values of two variables in the *iterator* section. Select **Run** to run the example code. After that you can modify the code and run it again.
9993

100-
[!code-csharp[csrefKeywordsIteration#8](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_2.cs)]
94+
[!code-csharp-interactive[not typical for loop example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#6)]
10195

102-
All of the expressions that define a `for` statement are optional. For example, the following statement creates an infinite loop:
96+
The following example defines the infinite `for` loop:
10397

104-
[!code-csharp[csrefKeywordsIteration#3](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_3.cs)]
98+
[!code-csharp[infinite for loop example](~/samples/snippets/csharp/keywords/IterationKeywordsExamples.cs#7)]
10599

106100
## C# language specification
107101

@@ -110,9 +104,9 @@ All of the expressions that define a `for` statement are optional. For example,
110104
## See also
111105

112106
[The for statement (C# language specification)](/dotnet/csharp/language-reference/language-specification/statements#the-for-statement)
113-
[C# Reference](../../../csharp/language-reference/index.md)
114-
[C# Programming Guide](../../../csharp/programming-guide/index.md)
115-
[C# Keywords](../../../csharp/language-reference/keywords/index.md)
116-
[foreach, in](../../../csharp/language-reference/keywords/foreach-in.md)
107+
[C# Reference](../index.md)
108+
[C# Programming Guide](../../programming-guide/index.md)
109+
[C# Keywords](index.md)
110+
[foreach, in](foreach-in.md)
117111
[for Statement (C++)](/cpp/cpp/for-statement-cpp)
118-
[Iteration Statements](../../../csharp/language-reference/keywords/iteration-statements.md)
112+
[Iteration Statements](iteration-statements.md)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.assetid: 7d494566-bf75-4ee8-979f-0f964209437e
88
---
99
# Iteration Statements (C# Reference)
1010

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.
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](jump-statements.md) is encountered.
1212

1313
The following keywords are used in iteration statements:
1414

@@ -21,6 +21,7 @@ The following keywords are used in iteration statements:
2121
- [while](while.md)
2222

2323
## See also
24+
2425
[C# Reference](../index.md)
2526
[C# Programming Guide](../../programming-guide/index.md)
2627
[C# Keywords](index.md)

docs/csharp/language-reference/keywords/nameof.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,22 @@ class C {
7979

8080
var c = new C()
8181

82-
nameof(C) -> "C"
83-
nameof(C.Method1) -> "Method1"
84-
nameof(C.Method2) -> "Method2"
85-
nameof(c.Method1) -> "Method1"
86-
nameof(c.Method2) -> "Method2"
87-
nameof(z) -> "z" // inside of Method2 ok, inside Method1 is a compiler error
88-
nameof(Stuff) = "Stuff"
89-
nameof(T) -> "T" // works inside of method but not in attributes on the method
90-
nameof(f) -> "f"
91-
nameof(f<T>) -> syntax error
92-
nameof(f<>) -> syntax error
93-
nameof(Method2()) -> error "This expression does not have a name"
82+
class Test {
83+
static void Main (string[] args) {
84+
Console.WriteLine(nameof(C)); // -> "C"
85+
Console.WriteLine(nameof(C.Method1)); // -> "Method1"
86+
Console.WriteLine(nameof(C.Method2)); // -> "Method2"
87+
Console.WriteLine(nameof(c.Method1)); // -> "Method1"
88+
Console.WriteLine(nameof(c.Method2)); // -> "Method2"
89+
// Console.WriteLine(nameof(z)); -> "z" [inside of Method2 ok, inside Method1 is a compiler error]
90+
Console.WriteLine(nameof(Stuff)); // -> "Stuff"
91+
// Console.WriteLine(nameof(T)); -> "T" [works inside of method but not in attributes on the method]
92+
Console.WriteLine(nameof(f)); // -> "f"
93+
// Console.WriteLine(nameof(f<T>)); -> [syntax error]
94+
// Console.WriteLine(nameof(f<>)); -> [syntax error]
95+
// Console.WriteLine(nameof(Method2())); -> [error "This expression does not have a name"]
96+
}
97+
}
9498
```
9599

96100
## Remarks

docs/csharp/programming-guide/indexers/indexers-in-interfaces.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ Indexers can be declared on an [interface](../../../csharp/language-reference/ke
2929
In the preceding example, you could use the explicit interface member implementation by using the fully qualified name of the interface member. For example:
3030

3131
```
32-
public string ISomeInterface.this[int index]
32+
string ISomeInterface.this[int index]
3333
{
3434
}
3535
```
3636

3737
However, the fully qualified name is only needed to avoid ambiguity when the class is implementing more than one interface with the same indexer signature. For example, if an `Employee` class is implementing two interfaces, `ICitizen` and `IEmployee`, and both interfaces have the same indexer signature, the explicit interface member implementation is necessary. That is, the following indexer declaration:
3838

3939
```
40-
public string IEmployee.this[int index]
40+
string IEmployee.this[int index]
4141
{
4242
}
4343
```
4444

4545
implements the indexer on the `IEmployee` interface, while the following declaration:
4646

4747
```
48-
public string ICitizen.this[int index]
48+
string ICitizen.this[int index]
4949
{
5050
}
5151
```

0 commit comments

Comments
 (0)