What is Console.ReadLine() in C#
The Console.ReadLine() is a C# predefined method that is used for reading a line of text entered by a user using a console application’s standard input stream (typically the keyboard). It enables a console-based app to take user input at runtime.
The Console.ReadLine() function reads the given input once it reaches the line’s end, which is normally indicated by the person pressing the enter key. After that, the method returns entered content in the form of a string, that can be saved in a variable or otherwise utilized for more processing.
Syntax
The following is the format for utilizing the Console.ReadLine() method:
The above declared a string variable as x and gives it the value of the subsequent line of text entered by the console by the user. The Console.ReadLine() function reads and returns just one line of text input as a string value. Additionally, the method forwards the display of input to the next line, ensuring that later calls to Console.ReadLine() which reads the following line of text input.
Parameters
The Console.ReadLine() require no arguments, and it just displays the subsequent line of text entered by the terminal by the user.
Return
The Console.ReadLine() method generates a string value representing the line of text sent into the console screen by the user. The function returns a string with no characters if the user inputs a blank line.
How to Use Console.ReadLine() in C#
Examples of using Console.ReadLine() method in C# are mentioned below:
Example 1
This method is used to prompt the user for input with Console.ReadLine() in C# as it allows you to ask the user for input via the console and then read the results into a variable containing strings in the below example code:
class Method
{
static void Main()
{
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");
}
}
This C# code demonstrates the use of the Console.ReadLine() function, which reads input from the user through the console. The program starts by importing the System namespace, and it then defines a class called “Method” with a static method called “Main” which serves as the entry point for the program.
The program prompts the user to enter a text by using the Console.Write() method to display the message “Enter your name: ” on the console.
The program then uses the Console.ReadLine() method to read the user’s input and store it in a string variable called name.
Finally, the program uses the Console.WriteLine() method to display the user’s input, along with an exclamation mark, on the console. The string interpolation syntax $”Hello, {name}!” is used to insert the value of the name variable into the string.
When a user types text and press the enter key, the program uses Console.ReadLine() function to get the input and then stores it in the name variable. Afterward, the input is then displayed the input with an exclamation mark on the console.
Example 2
In the following example, the program is taking user input and then uses conditional statements to tell if the entered age is greater than 18 or not:
class Method {
static void Main() {
Console.Write("Enter your age: ");
string ageInput = Console.ReadLine();
if (int.TryParse(ageInput, out int age)) {
if (age >= 18) {
Console.WriteLine("You are an adult.");
} else {
Console.WriteLine("You are not an adult.");
}
} else {
Console.WriteLine("Invalid input. Please enter a valid integer value.");
}
}
}
In the above code, tryParse() function is converting the user input which is a string into an integer for applying logical operations. The output would be:
Example 3
In the following example, the program is taking two numbers as input from the user and then prints result in the console after multiplying them:
class Method
{
static void Main()
{
Console.Write("Enter the first number: ");
string firstNumber = Console.ReadLine();
Console.Write("Enter the second number: ");
string secondNumber = Console.ReadLine();
if (int.TryParse(firstNumber, out int firstNum) && int.TryParse(secondNumber, out int secondNum))
{
int output = firstNum * secondNum;
Console.WriteLine($"The multiplication of {firstNumber} and {secondNumber} is {output}.");
}
else
{
Console.WriteLine("Invalid input. Please enter valid integer values.");
}
}
}
The output of the above code is given below:
Conclusion
The Console.ReadLine() function in C# is used for requesting input from the user and storing it in a variable that holds strings in the program. In general, the Console.ReadLine() is an indispensable function for each C# developer working on terminal apps. This article provides various methods to use this function in C#.