I have a small C# console app I am writing.
I would like the app to wait for instruction from the user regarding either a Y or a N keypress (if any other key is pressed the app ignores this and waits for either a Y or a N and then runs code dependent on the Y or N answer.
I came up with this idea,
while (true)
{
    ConsoleKeyInfo result = Console.ReadKey();
    if ((result.KeyChar == "Y") || (result.KeyChar == "y"))
    {
         Console.WriteLine("I'll now do stuff.");
         break;
    }
    else if ((result.KeyChar == "N") || (result.KeyChar == "n"))
    {
        Console.WriteLine("I wont do anything");
        break;
    }
}
Sadly though VS says its doesnt like the result.Keychat == as the operand cant be applied to a 'char' or 'string'
Any help please?
Thanks in advance.
KeyChar is a char while "Y" is a string.
You want something like KeyChar == 'Y' instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With