console.readline

why getting null value from console in c# for readLine() after using read()

那年仲夏 提交于 2019-11-29 15:00:23
I have the following code char c1 = (char)Console.Read(); Console.WriteLine("Enter a string."); string instr = Console.ReadLine(); It takes a value for c1 , after that it prints "Enter a string". However when I try to enter a string, it appears to be working like ReadKey() , meaning that as soon as I press any key it's showing that instr has a null value. If I remove the first line ( char c1 = (char)Console.Read(); ), program works correctly. Why is this? When you call Read() , it still blocks until you hit enter even though the actual method will only consume a single character from the input