C# Console.ReadLine Tips

by Sam Allen - Updated January 7, 2010

You want to read user input from the console in your C# program in the simplest way possible. The Console.ReadLine method in the System namespace in the .NET Framework allows you to read a string of characters, which you can then test and transform with other framework methods to accomplish this task. Here we look at how you can use the Console.ReadLine method to read in user input from the console window, seeing both a looping example and an example that parses input as integers, using the C# programming language.

Reading user input in console

Using Console.ReadLine

First, when developing testing programs you will often want to loop through user inputs, and this example shows how you can use a 'while (true)' loop and the Console.ReadLine method to do this. The example shows how you can test the string value of the result, and also how you can access the Length property of that string.

--- Program that uses Console.ReadLine loop (C#) ---

using System;

class Program
{
    static void Main()
    {
        while (true) // Loop indefinitely
        {
            Console.WriteLine("Enter input:"); // Prompt
            string line = Console.ReadLine(); // Get string from user
            if (line == "exit") // Check string
            {
                break;
            }
            Console.Write("You typed "); // Report output
            Console.Write(line.Length);
            Console.WriteLine(" character(s)");
        }
    }
}

--- Output of the program ---

Enter input:
Dot
You typed 3 character(s)
Enter input:
Net Perls
You typed 9 character(s)
Enter input:

Prompting user for string. The above program text defines the Main entry point, and in Main it loops infinitely while prompting the user in each iteration. The program shows the length property access on the string type for each string typed. If the user types "exit", then the program terminates immediately.

Reading integer from console

Here we see how you can use the string result variable from the Console.ReadLine parameterless method as an integer value. You can invoke the int.TryParse method to see if the result string is an integer representation, and it will return the value of that integer if possible. This program tries to multiply an integer value received by the user by 10 and display the product.

~~~ Program that parses Console input (C#) ~~~

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Type an integer:");
        string line = Console.ReadLine(); // Read string from console
        int value;
        if (int.TryParse(line, out value)) // Try to parse the string as an integer
        {
            Console.Write("Multiply integer by 10: ");
            Console.WriteLine(value * 10); // Multiply the integer and display it
        }
        else
        {
            Console.WriteLine("Not an integer!");
        }
    }
}

~~~ Output of the program ~~~

Type an integer:
4356
Multiply integer by 10: 43560

Parsing line as integer. The program text contains the Main entry point, which prompts the user for an input. The string variable with the identifier 'line' is then assigned to the reference of the string data allocated by Console.ReadLine and filled with the user's input. The int.TryParse static method then tests for a numeric value, and if this test succeeds we can then use the integer.

(See String Number, Testing Numeric String Values.)

Pausing before exit

Here we note a useful trick that you can use in your C# programs to prevent the console windows from disappearing rudely when the programs are done executing. You can insert a Console.ReadLine() method call at the end of the Main method (or even in a finally block in the Main method). This will ensure the terminal window is never dismissed by Windows immediately on program completion.

Summary

Here we looked at the Console.ReadLine method in the C# programming language targeting the .NET Framework. First we saw an example of using the Console.ReadLine method to accept input in a loop and testing the result each time; second we saw how you can parse the result of Console.ReadLine into an integer type for numerical processing. The Console.ReadLine method is very useful in many console programs and is fairly simple to use.

(Do not copy this page.)

Dot Net Perls | Search
Console | Console Color, Text and BackgroundColor | Console.Read Method | Console.ReadKey Method | Console.Write Example | Console.WriteLine Use
C# | Parameter Optimization Tip | SaveFileDialog Tutorial | IntegralHeight Property (Windows Forms) | Array.FindIndex Method
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen