Skip to content

Possibly ambiguous usage of break in C# switch statement #9566

Closed

Description

@ryanwebjackson

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.

Activity

  1. BillWagner commented on Jan 14, 2019

    @BillWagner
    Member

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

  2. added
    needs-more-info
    on Jan 14, 2019
  3. ryanwebjackson commented on Jan 22, 2019

    @ryanwebjackson
    Author

    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.

  4. added
    help wanted
    and removed
    needs-more-info
    on Mar 4, 2019
  5. BillWagner commented on Mar 4, 2019

    @BillWagner
    Member

    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.
  6. added this to the Backlog milestone on Mar 4, 2019
  7. changed the title [-]Possibly ambiguous usage[/-] [+]Possibly ambiguous usage of `break` in C# `switch` statement[/+] on Mar 11, 2019
  8. davidlfox commented on Oct 8, 2019

    @davidlfox
    Contributor

    @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?

  9. mairaw commented on Oct 8, 2019

    @mairaw
    Contributor

    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.

  10. removed this from the Backlog milestone on Oct 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dotnet-csharp/svchelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions