Search code examples
c#console.readkey

Why does Console.ReadKey().Key.ToString get lower and uppercase Letter by pressing once?


This snipet Outputs two Letters when i press one Key (r).

string key = string.Empty;
key = Console.ReadKey().Key.ToString();
Console.WriteLine(key);
Console.ReadKey();
// Output: rR

Why does it output lower 'r' and Upercase 'R' when i only press once the lowercase 'r'?


Solution

  • The lower case r is what you have written from keyboard. You do not remove it form console by reading it. And the capital letter R is the character representation of the Key from ConsoleKeyInfo structure. If you want to read exactly what was written to console use Console.Read() or Console.ReadLine().