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
Way to read input from console in C#
The Console.ReadLine() method is the primary way to read input from the console in C#. This method reads a complete line of text from the console and returns it as a string. Since all console input is received as text, you need to convert it to the appropriate data type when working with numbers or other non-string values.
Syntax
Following is the syntax for reading console input −
string input = Console.ReadLine();
For converting string input to other data types −
int number = Convert.ToInt32(input); // Convert to integer double value = Convert.ToDouble(input); // Convert to double bool flag = Convert.ToBoolean(input); // Convert to boolean
Reading and Converting Integer Input
The most common scenario involves reading numeric input. Here's how to read and convert string input to an integer −
using System;
class Demo {
static void Main() {
Console.Write("Enter an integer: ");
string input = Console.ReadLine();
int number = Convert.ToInt32(input);
Console.WriteLine("You entered: " + number);
Console.WriteLine("Double the value: " + (number * 2));
}
}
The output of the above code is −
Enter an integer: 25 You entered: 25 Double the value: 50
Reading Multiple Data Types
You can read and convert different data types from console input as needed −
using System;
class InputExample {
static void Main() {
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.Write("Enter your age: ");
int age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter your salary: ");
double salary = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("<br>--- Your Information ---");
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age + " years");
Console.WriteLine("Salary: $" + salary);
}
}
The output of the above code is −
Enter your name: John Smith Enter your age: 28 Enter your salary: 45000.50 --- Your Information --- Name: John Smith Age: 28 years Salary: $45000.5
Using Alternative Parsing Methods
Besides Convert methods, you can also use Parse() and TryParse() methods for type conversion −
using System;
class ParseExample {
static void Main() {
Console.Write("Enter a number: ");
string input = Console.ReadLine();
// Using Parse method
int number1 = int.Parse(input);
Console.WriteLine("Using Parse: " + number1);
// Using TryParse method (safer)
if (int.TryParse(input, out int number2)) {
Console.WriteLine("Using TryParse: " + number2);
Console.WriteLine("Parsing successful!");
} else {
Console.WriteLine("Invalid input format");
}
}
}
The output of the above code is −
Enter a number: 42 Using Parse: 42 Using TryParse: 42 Parsing successful!
Comparison of Conversion Methods
| Method | Description | Exception Handling |
|---|---|---|
Convert.ToInt32() |
Converts string to integer, handles null values | Throws exception on invalid input |
int.Parse() |
Parses string to integer | Throws exception on invalid input |
int.TryParse() |
Safely attempts to parse string to integer | Returns false on failure, no exception |
Conclusion
The Console.ReadLine() method is essential for reading user input in C# console applications. Always remember to convert the string input to the appropriate data type using methods like Convert.ToInt32(), Parse(), or the safer TryParse() method when working with numeric values.
