HOWTO · Csharp
How to Read Integer From Console in C#
This article explores various methods for reading integer values from the console in C#, providing insights into when and how to use each approach.
On this page
To read an integer from a C# console, first read a line with Console.ReadLine(), then convert its text. Use int.TryParse() when users may enter unexpected text; it reports whether conversion worked and keeps ordinary bad input in the normal control flow. This guide compares int.Parse(), int.TryParse(), and Convert.ToInt32() for line-based input.
Read an integer from the console with int.Parse()
int.Parse() converts a string containing a valid 32-bit integer into an int. It is concise when the input is trusted or when your surrounding code handles conversion errors.
This complete program reads a line and prints the converted value:
using System;
using System.IO;
class Program
{
static void Main()
{
Console.SetIn(new StringReader("42"));
Console.Write("Enter an integer: ");
string input = Console.ReadLine();
int value = int.Parse(input);
Console.WriteLine($"You entered: {value}");
}
}
Enter an integer: You entered: 42
Text that is not an integer causes FormatException; a value outside the Int32 range causes OverflowException. In a real console session, Console.ReadLine() returns null if the input stream ends before a line is read. Passing that value to int.Parse() causes ArgumentNullException, so check for null first when end-of-input is possible. The sample supplies 42 through a StringReader so it runs as a self-contained example.
Read an integer from the console with int.TryParse()
int.TryParse() is usually the clearest choice for interactive input because conversion success is part of normal control flow. It returns true and assigns the converted number to the out variable when the text represents an in-range integer. Otherwise, it returns false and assigns the default value (0) to that variable; check the Boolean result before using the number.
using System;
using System.IO;
class Program
{
static void Main()
{
Console.SetIn(new StringReader("42"));
Console.Write("Enter an integer: ");
string input = Console.ReadLine();
int value;
if (int.TryParse(input, out value))
{
Console.WriteLine($"You entered: {value}");
}
else
{
Console.WriteLine("Enter a whole number within the Int32 range.");
}
}
}
Enter an integer: You entered: 42
For nonnumeric text, end-of-input, or a value outside the int range, TryParse returns false, so the program prints the message in the else branch. You can put the prompt and parsing check in a loop to let a person try again; this example gives one response and exits. The StringReader supplies 42 here to keep the sample self-contained.
Read an integer from the console with Convert.ToInt32()
Convert.ToInt32() is another way to convert the line returned by Console.ReadLine(). For numeric text it produces the same int value as int.Parse(). It does not make arbitrary text safe: nonnumeric text can cause FormatException, and a value outside the Int32 range can cause OverflowException. A null string is a special case: Convert.ToInt32((string)null) returns 0, while int.Parse(null) throws ArgumentNullException.
using System;
using System.IO;
class Program
{
static void Main()
{
Console.SetIn(new StringReader("42"));
Console.Write("Enter an integer: ");
string input = Console.ReadLine();
int value = Convert.ToInt32(input);
Console.WriteLine($"You entered: {value}");
}
}
Enter an integer: You entered: 42
Choose int.TryParse() when input is supplied by a person and a normal validation path is useful. Use int.Parse() or Convert.ToInt32() when conversion errors should be handled by the caller. For these console examples, all three methods start with Console.ReadLine(); they differ in how conversion results and special input values are handled.