I have a pop for a user to enter a username and then what group to be added to in AD. I can't think of another way to exit my loop other than doing two separate if-statements. Is there a different way to exit the loop other than this? or I am overthinking this?
bool isExit = false;
do
{
Console.Write("Enter the username(Type 'exit' to go back to menu): ");
string username = Console.ReadLine();
if(username.ToLower() == "exit")
{
isExit = true;
}
Console.Write("Enter the group name (Type 'exit' to go back to menu): ");
string groupName = Console.ReadLine();
if(groupName.ToLower() == "exit")
{
isExit = true;
}
}while(!IsExit);
Unfortunately C# doesn't support non-local returns (so I'm envious of Kotlin users), but you can refactor your logic down to a single reusable loop that you can move into its own function - just reserve
nullas a special-value for the quit/exit case:If you're using a semi-recent version of C#, you can refactor it into a
static-local function which is handy - but a class-levelstaticmethod is fine too.