It is an expected behavior.
ReadLine
will read characters until it finds '\n'
in the other hand Read
will not read '\n'
so when you read the command r you will have a '\n'
in your buffer and so you arrive in ReadLine
and pass directly.
so you have to clean the buffer a simple ReadLine
will do just that.
class Test
{
static void PrintName()
{
Console.Out.Write("Enter your name: ");
string name = Console.In.ReadLine();
Console.WriteLine(name);
Console.Out.Write("\nEnter R to restart: ");
char r = Convert.ToChar(Console.In.Read());
if (r.ToString().Equals("r", StringComparison.OrdinalIgnoreCase))
{
Console.In.ReadLine();
PrintName();
}
else
Environment.Exit(0);
}
static void Main(string[] args)
{
PrintName();
}
}