Jump to content

C# how to exit console with specific command?

WereCat
Go to solution Solved by colonel_mortis,

I think this should be what you want

string response = Console.ReadLine();if (response.ToLower() == "quit") { //Doesn't matter whether they use upper or lower case  //quit} else {  //continue}

Hi, I recently started learning in C# but I cant figure out how to do this. I am just playing around with code so far and barely just scratched basics and I know already how to make simple stuff but now I would like to make something more user friendly but I want to make such console that can exit on command.

I know that I can write at the end of the code:

Console.ReadKey ();

and then press anything and it will exit but I want to make it so it will exit only when it is typed "quit" or when the console asks you if you want to exit or continue.

 

Console.WriteLine ("Do you wish to exit or continue?\nPress E if you wish to exit or press C if you wish to continue");

 

if(condition to press C) {

// continue

} else if (condition to press E) {

// exit ... I dont want this to be Console.ReadKey (); to press any key to exit

} else {

Console.WriteLine ("You entered bad input");

}

Link to comment
Share on other sites

Link to post
Share on other sites

I think this should be what you want

string response = Console.ReadLine();if (response.ToLower() == "quit") { //Doesn't matter whether they use upper or lower case  //quit} else {  //continue}
Edited by colonel_mortis

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

 

I think this should be what you want

string response = Console.ReadLine();if (response.ToLower == "quit") { //Doesn't matter whether they use upper or lower case  //quit} else {  //continue}

Thanks, that helped but I had to delete .ToLower because I was geting error that operator '==' cannot be applied to operands of type 'method group' and 'string'.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks, that helped but I had to delete .ToLower because I was geting error that operator '==' cannot be applied to operands of type 'method group' and 'string'.

Oh yeah I forgot to add the () to the end of that command. I've updated it now, and that version should hopefully work.

Alternatively, if you're happy with it being case sensitive, it's fine without it.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Oh yeah I forgot to add the () to the end of that command. I've updated it now, and that version should hopefully work.

Alternatively, if you're happy with it being case sensitive, it's fine without it.

Yep, it does work. Once again, thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Best practice for comparing strings like this and ignoring the case is by using C#'s built in StringComparison comparers.

var stringLower = "string";var stringUpper = "STRING";var sameLetters = stringLower.Equals(stringUpper, StringComparison.OrdinalIgnoreCase); // true
Link to comment
Share on other sites

Link to post
Share on other sites

 

Best practice for comparing strings like this and ignoring the case is by using C#'s built in StringComparison comparers.

 

Agreed. I would also use this method.

 

WereCat: Alternatively you can write it like this.

String.Equals(stringLower, stringUpper, StringComparison.OrdinalIgnoreCase)

Both work the same way. Sometimes I find one is more readable than the other. It's mostly personal preference though. Just wanted to let you know the option was there.

Link to comment
Share on other sites

Link to post
Share on other sites

Agreed. I would also use this method.

WereCat: Alternatively you can write it like this.

String.Equals(stringLower, stringUpper, StringComparison.OrdinalIgnoreCase)
Both work the same way. Sometimes I find one is more readable than the other. It's mostly personal preference though. Just wanted to let you know the option was there.
@DavidTheWin

Thanks. I appreciate the help. I will try it after work.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×