If you ever want to read only a single character don't use
Read()
as you'll be getting more problems with it than it's good for you.
Console.ReadKey()
is the way to go in your case:
Console.Write("Enter first char: ");
char a = Console.ReadKey().KeyChar;
Console.Write("\nEnter second char: ");
char b = Console.ReadKey().KeyChar;
Console.WriteLine("\na-" + a.ToString() + ",b-" + b.ToString() + "");
Console.ReadLine();
While read will fetch the next character from the input stream, it will also block until a new line is entered. This new line will remain in the input queue so your subsequent
Read()
will fetch that. See below for what MSDN has to say about
Read()
.
The MSDN documentation has this to say:
"The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.
Note that you will not get a property value of -1 unless you perform one of the following actions: simultaneously press the Control modifier key and Z console key (Ctrl+Z), which signals the end-of-file condition; press an equivalent key that signals the end-of-file condition, such as the F6 function key in Windows; or redirect the input stream to a source, such as a text file, that has an actual end-of-file character."
Regards,
— Manfred