Using the Console
Reading from the Console using Read
We saw previously how to read from the Console using the ReadLine method. Remember how this method reads the an entire line at a time and returns you the input text? Another common operation is the Read method. Unlike ReadLine, the read method returns one character at a time until it reaches the end of the input. This includes, letters, numbers, line breaks, and all other characters.
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Say anything!");
int value = Console.Read();
Console.Write("You typed: " + (char)value);
}
}
One of the big differences between Read and ReadLine is that Read doesn't actually return a string, it returns an integer, moreover it returns the ASCII representation of the character you entered. You can find more information about the ASCII character code system in the deep dive section of this tutorial.
To obtain the actual character entered, we need to convert this integer into a character. You can do this a couple of ways, but the simplest, easiest approach is to perform a cast:
int value = 65; // The ASCII code for 'A'
char letter = (char)value;
NOTE: Currently .NET Fiddle does not support the Console.Read operation.