Skip to content

Commit 56bf8bc

Browse files
committed
Add example for unreachable code
Fixes dotnet#937 I admit this rule confused me, and I asked to add the example. After reading the updated, I'm ambivalent about adding the example. I don't want to move it into the bullet list, as that breaks the flow even more. Let's discuss if it's needed at the meeting.
1 parent 1b8e8c3 commit 56bf8bc

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

standard/variables.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,19 @@ The definite-assignment state of *v* on the control flow transfer to a reachable
343343
- If *v* is a pattern variable declared in the *switch_label*: “definitely assigned”.
344344
- Otherwise, the state of *v* is the same as the stat of *v* after *expr*.
345345
346+
> *Example*: The third rule eliminates the need for the compiler to warn if an unassigned variable is accessed in unreachable code. The state of *b* is "definitely assigned" in the unreachable switch label `case 2 when b`.
347+
>
348+
> ```csharp
349+
> bool b;
350+
> switch (1)
351+
> {
352+
> case 2 when b: // b is definitely assigned here.
353+
> break;
354+
> }
355+
> ```
356+
>
357+
> *end example*
358+
346359
A consequence of these rules is that a pattern variable declared in a *switch_label* will benot definitely assignedin the statements of its switch section if it is not the only reachable switch label in its section.
347360
348361
> *Example*:

0 commit comments

Comments
 (0)