C# Read User Input from the Console

In C#, you can read text entered by a user with the Console.ReadLine() method. The method waits until the user types a value and presses Enter, then returns the entered line as a string.

The returned value can be stored in a variable, displayed, validated, or converted to another data type such as int, double, or decimal.

C# Console.ReadLine() Syntax

The basic syntax for reading one line of console input is:

</>
Copy
string? value = Console.ReadLine();

Console.ReadLine() returns the text entered before the user presses Enter. In nullable-enabled C# projects, its return type is string? because the method can return null when no more input is available.

Console.Write() is commonly used before Console.ReadLine() to display a prompt on the same line. Console.WriteLine() can also display a prompt, but it moves the cursor to the next line.

Read a String from the C# Console

By default Console.ReadLine() reads string, from the console, entered by user. We are storing the value into a string and printing it in the next statement.

C# Program

</>
Copy
using System;

namespace CSharpExamples {

    class Program {
        static void Main(string[] args) {
            Console.Write("Enter string: ");
            String str = Console.ReadLine();
            Console.WriteLine("You entered \""+str+"\" in the console.");
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
Enter string: TutorialKart
You entered "TutorialKart" in the console.

The entered text is assigned to str. The program then combines the stored value with another string and prints the result.

Read an Integer from the C# Console

Console.ReadLine() always reads the input as text. To store a whole number in an int variable, convert the returned string to an integer.

C# Program

</>
Copy
using System;

namespace CSharpExamples {

    class Program {
        static void Main(string[] args) {
            Console.Write("Enter number: ");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You entered the number, "+n+" in console.");
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
Enter number: 65
You entered the number, 65 in console.

Convert.ToInt32() converts the entered text to an integer. If the user enters text that is not a valid integer, the conversion throws an exception.

Validate Console Input with int.TryParse()

Use int.TryParse() when invalid user input should be handled without an exception. It returns true when the conversion succeeds and false when it fails.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter an integer: ");
        string? input = Console.ReadLine();

        if (int.TryParse(input, out int number))
        {
            Console.WriteLine($"You entered {number}.");
        }
        else
        {
            Console.WriteLine("The entered value is not a valid integer.");
        }
    }
}

Output for valid input

Enter an integer: 42
You entered 42.

Output for invalid input

Enter an integer: forty-two
The entered value is not a valid integer.

Keep Asking Until the User Enters a Valid Integer

A loop can repeat the prompt until the user provides acceptable input. This pattern is useful in menu-driven programs and console forms.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        int number;

        while (true)
        {
            Console.Write("Enter an integer from 1 to 10: ");
            string? input = Console.ReadLine();

            if (int.TryParse(input, out number) && number >= 1 && number <= 10)
            {
                break;
            }

            Console.WriteLine("Enter a whole number between 1 and 10.");
        }

        Console.WriteLine($"Accepted value: {number}");
    }
}

The condition checks both the data type and the permitted range. The loop ends only after the input can be converted to an integer between 1 and 10.

Read a Decimal Number from the C# Console

Use double.TryParse() for general decimal values or decimal.TryParse() when decimal precision is required, such as for monetary calculations.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a price: $");
        string? input = Console.ReadLine();

        if (decimal.TryParse(input, out decimal price))
        {
            Console.WriteLine($"Entered price: {price:C}");
        }
        else
        {
            Console.WriteLine("Enter a valid decimal amount.");
        }
    }
}
Enter a price: $19.95
Entered price: $19.95

Number parsing follows the current culture of the application. The accepted decimal separator can therefore vary depending on the system or configured culture.

Read a Single Character with Console.Read()

Console.Read() reads the next character from the standard input stream and returns its numeric character code as an int. Convert the result to char when the actual character is needed.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a character: ");
        int value = Console.Read();

        if (value != -1)
        {
            char character = (char)value;
            Console.WriteLine($"\nYou entered: {character}");
        }
    }
}

For ordinary line-based input, Console.ReadLine() is usually simpler. Use Console.Read() when only the next character is required.

Read a Key Press with Console.ReadKey()

Console.ReadKey() reads one key press immediately, without requiring Enter. It returns a ConsoleKeyInfo value containing the pressed character, key, and modifier information.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        Console.Write("Press Y to continue: ");
        ConsoleKeyInfo keyInfo = Console.ReadKey(intercept: true);

        if (keyInfo.Key == ConsoleKey.Y)
        {
            Console.WriteLine("\nContinuing...");
        }
        else
        {
            Console.WriteLine("\nOperation cancelled.");
        }
    }
}

Passing true to the intercept parameter prevents the pressed key from being displayed in the console.

Read Multiple C# Console Values from One Line

When several values are entered on one line, read the complete line and split it using a separator such as a space or comma.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter two integers separated by a space: ");
        string? input = Console.ReadLine();
        string[] parts = (input ?? string.Empty).Split(
            ' ',
            StringSplitOptions.RemoveEmptyEntries
        );

        if (parts.Length == 2 &&
            int.TryParse(parts[0], out int first) &&
            int.TryParse(parts[1], out int second))
        {
            Console.WriteLine($"Sum: {first + second}");
        }
        else
        {
            Console.WriteLine("Enter exactly two valid integers.");
        }
    }
}
Enter two integers separated by a space: 12 8
Sum: 20

Console.ReadLine(), Console.Read(), and Console.ReadKey()

C# console methodWhat it readsReturn valueTypical use
Console.ReadLine()A complete line ending when Enter is pressedstring?Names, sentences, numbers entered as text, and form-style input
Console.Read()The next character from standard inputintCharacter-by-character input
Console.ReadKey()One key press without waiting for EnterConsoleKeyInfoMenus, confirmations, and pause prompts

Common C# Console Input Errors

  • Assuming Console.ReadLine() returns a number: It returns text, so numeric input must be parsed or converted.
  • Using Convert.ToInt32() for untrusted input: Invalid text can cause an exception. Use int.TryParse() when input may be incorrect.
  • Ignoring null input: In nullable-enabled code, handle the possibility that Console.ReadLine() returns null.
  • Accepting an empty value unintentionally: Check string.IsNullOrWhiteSpace() when blank input is not allowed.
  • Parsing culture-sensitive values incorrectly: Decimal separators and number formats may depend on the active culture.

C# Console Input Frequently Asked Questions

Why does Console.ReadLine() return a string?

Console input arrives as characters. Console.ReadLine() therefore returns the entered line as text. Convert or parse that text when the program requires another data type.

What is the safest way to read an integer in C#?

Use int.TryParse(Console.ReadLine(), out int number). It reports whether the conversion succeeded without throwing an exception for invalid input.

Does Console.ReadLine() include the Enter key?

No. Pressing Enter completes the input operation, but the returned string does not contain the line-ending characters.

How do you prevent blank console input in C#?

Read the value and test it with string.IsNullOrWhiteSpace(). If the result is true, display an error or repeat the prompt.

C# Console Input Tutorial Review Checklist

  • Confirm that each example treats Console.ReadLine() as string input before conversion.
  • Use TryParse() in examples where users may enter invalid numeric values.
  • Check that nullable console input is handled in newly added C# examples.
  • Verify that displayed output matches the exact prompt and input used in each program.
  • Use Console.ReadKey() only when a single immediate key press is required.

Reading User Input in C# Console Programs

In this C# Tutorial, we read input entered by the user in console using Console.ReadLine() function.