Reading Console Input

Console.In is an instance of TextReader, and you can use the methods and properties defined by TextReader to access it. However, you will usually use the methods provided by Console, which automatically read from Console.In. Console defines three input methods. The first two, Read( ) and ReadLine( ), have been available since .NET Framework 1.0. The third, ReadKey( ), was added by .NET Framework 2.0.
 
To read a single character, use the Read( ) method:
static int Read( )
Read( ) returns the next character read from the console. It waits until the user presses a key and then returns the result. The character is returned as an int, which must be cast to char. Read( ) returns –1 on error. This method will throw an IOException on failure. When using Read( ), console input is line-buffered, so you must press enter before any character that you type will be sent to your program. Here is a program that reads a character from the keyboard using Read( ):

Example

using System;

namespace ConsoleApplication1
{
    class KbIn
    {
        static void Main()
        {
            char ch;
            Console.Write("Press a key followed by ENTER: ");
            ch = (char)Console.Read(); // get a char
            Console.WriteLine("Your key is: " + ch);
        }
    }
}

The fact that Read( ) is line-buffered is a source of annoyance at times. When you press enter, a carriage-return, line-feed sequence is entered into the input stream. Furthermore, these characters are left pending in the input buffer until you read them. Thus, for some applications, you may need to remove them (by reading them) before the next input operation. (To read keystrokes from the console in a non-line-buffered manner, you can use ReadKey( )) To read a string of characters, use the ReadLine( ) method. It is shown here:

static string ReadLine( )

ReadLine( ) reads characters until you press enter and returns them in a string object. This method will throw an IOException if an I/O error occurs.
Here is a program that demonstrates reading a string from Console.In by using ReadLine( ):

Example

using System;

namespace ConsoleApplication1
{
    class ReadString
    {
        static void Main()
        {
            string str;
            Console.WriteLine("Enter some characters.");
            str = Console.ReadLine();
            Console.WriteLine("You entered: " + str);
        }
    }
}

Although the Console methods are the easiest way to read from Console.In, you can call methods on the underlying TextReader. For example, here is the preceding program rewritten to use the ReadLine( ) method defined by TextReader:

Example

using System;

namespace ConsoleApplication1
{
    // Read a string from the keyboard, using Console.In directly
    class ReadChars2
    {
        static void Main()
        {
            string str;
            Console.WriteLine("Enter some characters.");
            str = Console.In.ReadLine(); // call TextReader's ReadLine() method
            Console.WriteLine("You entered: " + str);
        }
    }
}

Notice how ReadLine( ) is now invoked directly on Console.In. The key point here is that if you need access to the methods defined by the TextReader that underlies Console.In, you will invoke those methods as shown in this example.