C# - Console IO

To read a line of text from the console window, you use the Console.ReadLine() method. This will read an input stream (terminated when the user presses the Return key) from the console window and return the input string. There are also two corresponding methods for writing to the console, which you have already used extensively:

  • Console.Write() — Writes the specified value to the console window.
  • Console.WriteLine() - Writes the specified value to the console window but adds a newline character at the end of the output.

Various forms (overloads) of these methods exist for all the predefined types (including object), so in most cases you don’t have to convert values to strings before you display them.

For example, the following code lets the user input a line of text and displays that text:

    string s = Console.ReadLine();
    Console.WriteLine(s);