Why is not readable () working as expected?

I am using Visual Studio 2008 for C #. I do not understand why this simple code does not work as expected. Any ideas? Thanks!

using System; namespace TryRead { class Program { static void Main() { int aNumber; Console.Write("Enter a single character: "); aNumber = Console.Read(); **//Program waits for [Enter] key. Why?** Console.WriteLine("The value of the character entered: " + aNumber); Console.Read(); **//Program does not wait for a key press. Why?** } } } 
+4
source share
2 answers

// The program waits for the [Enter] key. Why?

The Read method blocks its return when entering characters; it ends when you press the Enter key. Pressing Enter adds a platform-specific line-termination sequence to your input (for example, Windows adds a carriage-return sequence).

// The program does not wait for the key Press. Why?

Subsequent calls to the Read method retrieve your input one character at a time [without blocking]. After receiving the final Read character, it blocks its return again and the cycle repeats.

http://msdn.microsoft.com/en-us/library/system.console.read.aspx

+4
source

You need to use Console.ReadKey () instead of Console.Read ().

+2
source

All Articles