Docs: Possibly ambiguous usage of `break` in C# `switch` statement

Created on 12 Dec 2018  Â·  5Comments  Â·  Source: dotnet/docs

I know this might be a "gotcha", but within the context of a switch statement inside of a loop, to a human, it can be confusing what "break" is referring to.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide P2 up-for-grabs

Most helpful comment

Either in the commit message or in the PR description is enough. You can then create the docs PR right after, just call out that is dependent on the samples PR you created and we'll make sure to review after the samples PR is merged.

All 5 comments

@ryanwebjackson You are correct.

Is there more information that you need for this than what is available in the 2nd sample? That break breaks out of the inner loop, but not the outer loop.

I was referring to a _switch_ statement inside of a loop, so an example of that would be perfect. Two loops can make it easier to tell what's going on, because you need curly brackets for both (if they're more than one line of code). I don't have access to the code I was looking at when I opened this, but because there can be both breaks (switch and loop) in the same code block -- that syntax can be confusing.

The right fix is to make the following changes:

  • [ ] Move the switch example before the nested loop example.
  • [ ] Add text introducing the last (nested loop) example stating that 'break` exits the current branch or loop statement. Reinforce it by stating that this means only one level.
  • [ ] add a final example with a lop and an embedded switch statement.

@BillWagner When adding a new snippet to the dotnet/samples repository, should this issue be referenced in commit message, then a PR submitted to samples, _then_ a PR submitted to dotnet/docs repository for this issue?

Either in the commit message or in the PR description is enough. You can then create the docs PR right after, just call out that is dependent on the samples PR you created and we'll make sure to review after the samples PR is merged.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skylerberg picture skylerberg  Â·  3Comments

ike86 picture ike86  Â·  3Comments

sebagomez picture sebagomez  Â·  3Comments

sime3000 picture sime3000  Â·  3Comments

sdmaclea picture sdmaclea  Â·  3Comments