Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to read inputs as integers in C#?
To read inputs as integers in C#, you need to first read the input as a string using Console.ReadLine() and then convert it to an integer using methods like Convert.ToInt32() or int.Parse().
The process involves two steps: reading the user input as a string and converting it to an integer data type for mathematical operations.
Syntax
Following is the basic syntax for reading integer input −
string input = Console.ReadLine(); int number = Convert.ToInt32(input);
You can also use int.Parse() method −
int number = int.Parse(Console.ReadLine());
Using Convert.ToInt32() Method
The Convert.ToInt32() method converts the specified string representation of a number to an equivalent 32-bit signed integer. This method handles null values gracefully by returning 0.
Example
using System;
class Demo {
static void Main() {
Console.WriteLine("Enter a number: ");
string input = Console.ReadLine();
// Convert string to integer
int number = Convert.ToInt32(input);
// Perform calculations
int doubled = number * 2;
int squared = number * number;
Console.WriteLine("You entered: " + number);
Console.WriteLine("Doubled: " + doubled);
Console.WriteLine("Squared: " + squared);
}
}
The output of the above code when user enters 5 −
Enter a number: You entered: 5 Doubled: 10 Squared: 25
Using int.Parse() Method
The int.Parse() method directly converts a string to an integer. It is more strict than Convert.ToInt32() and throws an exception for null values.
Example
using System;
class Program {
static void Main() {
Console.WriteLine("Enter first number: ");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter second number: ");
int num2 = int.Parse(Console.ReadLine());
int sum = num1 + num2;
int product = num1 * num2;
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Product: " + product);
}
}
The output of the above code when user enters 8 and 3 −
Enter first number: Enter second number: Sum: 11 Product: 24
Using TryParse() for Error Handling
The int.TryParse() method provides a safe way to convert strings to integers without throwing exceptions for invalid input.
Example
using System;
class SafeInput {
static void Main() {
Console.WriteLine("Enter a number: ");
string input = Console.ReadLine();
if (int.TryParse(input, out int result)) {
Console.WriteLine("Valid number: " + result);
Console.WriteLine("Number + 10 = " + (result + 10));
} else {
Console.WriteLine("Invalid input. Please enter a valid integer.");
}
}
}
The output of the above code when user enters "abc" −
Enter a number: Invalid input. Please enter a valid integer.
Comparison of Methods
| Method | Behavior with Null | Exception on Invalid Input | Best Use Case |
|---|---|---|---|
| Convert.ToInt32() | Returns 0 | Yes | When input might be null |
| int.Parse() | Throws exception | Yes | When input is guaranteed valid |
| int.TryParse() | Returns false | No | When input validation is needed |
Conclusion
Reading integer inputs in C# requires converting string input from Console.ReadLine() to integer using Convert.ToInt32(), int.Parse(), or int.TryParse(). For robust applications, use int.TryParse() to handle invalid input gracefully without exceptions.
