Search code examples
c#syntaxgoto

C# goto use - what else to use here?


I know using goto is something most people say to avoid, however I have read on various places that sometimes it is useful if you need simple code. Currently I have very simple program that needs to be repeated if user selects so:

static void Main()
{
    Restart:
    ...

    string UserChoice=Console.ReadLine();
    if (UserChoice=="y")
    goto Restart;
}

Is using goto here really so bad? I just cannot see any other way how to repeat the code without doing loops etc. This seems to be very straightforward and clean way. Or am I missing something?


Solution

  • string userchoice;
    
    do {                
    
        userchoice=Console.ReadLine();
    
    } while (userchoice=="y");