Skip to content

Commit feaeb0f

Browse files
committed
Add extra example in switch/case
Let's wait until next meeting before merging this, as I worry that it might muddy the waters a bit.
1 parent 2239f60 commit feaeb0f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

standard/basic-concepts.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,24 @@ There are several different types of declaration spaces, as described in the fol
9292
- The syntactic translation of a *query_expression* ([§12.20.3](expressions.md#12203-query-expression-translation)) may introduce one or more lambda expressions. As anonymous functions, each of these creates a local variable declaration space as described above.
9393
- Each *block* or *switch_block* creates a separate declaration space for labels. Names are introduced into this declaration space through *labeled_statement*s, and the names are referenced through *goto_statement*s. The ***label declaration space*** of a block includes any nested blocks. Thus, within a nested block it is not possible to declare a label with the same name as a label in an enclosing block.
9494

95-
> *Note*: The fact that variables declared directly within a *switch_section* are added to the local variable declaration space of the *switch_block* instead of the *switch_section* can lead to surprising code. In the example below, the local variable `y` is in scope within the switch section for the default case, despite the declaration appearing in the switch section for case 0.
95+
> *Note*: The fact that variables declared directly within a *switch_section* are added to the local variable declaration space of the *switch_block* instead of the *switch_section* can lead to surprising code. In the example below, the local variable `y` is in scope within the switch section for the default case, despite the declaration appearing in the switch section for case 0. The local variable `z` is not in scope within the switch section for the default case, as it is introduced in the local variable declaration space for the switch section in which the declaration occurs.
9696
>
97-
> <!-- Example: {template:"code-in-main", name:"SwitchSurprise", expectedOutput:["11"]} -->
97+
> <!-- Example: {template:"code-in-main", name:"SwitchSurprise", expectedErrors:["CS0103"]} -->
9898
> ```csharp
9999
> int x = 1;
100100
> switch (x)
101101
> {
102102
> case 0:
103103
> int y;
104104
> break;
105+
> case var z when z < 10:
106+
> break;
105107
> default:
106108
> y = 10;
109+
> // Valid: y is in scope
107110
> Console.WriteLine(x + y);
111+
> // Invalid: z is not scope
112+
> Console.WriteLine(x + z);
108113
> break;
109114
> }
110115
> ```

0 commit comments

Comments
 (0)