Using the Console class in C# is the simplest way to develop a C# program. Similar to the C# WriteLine() method, which is used to print the entire string on a simple line, the C# ReadLine() is a widely used method that belongs to the Console class of C# and is used in taking user input.
It accepts the entire string from the user and takes the cursor to the next line for accepting the follow-up inputs given by the user.
Uses of C# Readline Method
The C# readline method is mainly used to read the complete string until the user presses the Enter key or a newline character is found.Â
Using this method, each line from the standard data input stream can be read. It is also used to pause the console so that the user can take a look at the output.Â
Note: Type casting required for non-string data type - For any type of input data other than characters and strings, the read input has to be changed from string to the respective data type.
Syntax of C# Readline Method
The syntax for the C# readline method is -
public static string ReadLine ();
Also Read: What Is Data: Types of Data, and How to Analyze Data?
Type-Casting for Non-string Type Inputs for C# Readline Method
The default data type of the C# readline is string. Some of the typecasting methods for non-string input data types in order to convert them from string to the respective data types are as given below-
int data type
Method - Convert.ToInt32();
Syntax - Convert.ToInt32(Console.ReadLine());
double data type
Method - Convert.ToDouble();
Syntax - Convert.ToDouble(Console.ReadLine());
boolean data type
Method - Convert.ToBoolean();
Syntax - Convert.ToBoolean(Console.ReadLine());
char data type
Method - Convert.ToChar();
Syntax - Convert.ToChar(Console.ReadLine());
Exceptions That Might Occur While Using C# Readline
The following are the exceptions that might arise based on different situations/errors in C# -Â
- IOException - This exception arises whenever an Input-Output (I/O) error occurs.
- OutOfMemoryException - This exception arises whenever there is insufficient memory to allocate a buffer for the string that gets returned.
- ArgumentOutOfRangeException - This exception arises when the characters in the next line exceed the MaxValue.
Sample Code Examples of C# Readline
The following are some sample code examples to understand C# readline -Â
Example 1
Aim - Write a C# program that takes the input from the user using the C# ReadLine() method.
File name - Program1.cs
The following program takes the feeling of the user as input using C# ReadLine() method and prints it to the console using the C# WriteLine() method.
CodeÂ
using System;
using System.IO; // defining the System package
class Program1 {
public static void Main(string[] args) // defining the main function
{
string feeling; //declaring variable feeling
Console.WriteLine("Hey, how do you feel today?");
feeling = Console.ReadLine(); // takes the input from the user
Console.WriteLine("Hello there! I am feeling "+ feeling + "!"); // print the output
}
}
Output Â
Example 2
Aim - Write a C# program that takes in an integer as input from the user using the C# ReadLine() method.
File name - Program2.cs
The following program simulates a simple billing system of an ice-cream parlor. The inputs of the price of each ice cream, the number of ice-creams ordered by the customer, and the name of the customer are read using C# ReadLine(). The integer type inputs are converted from string to input and then stored in their respective variables. The final output is printed using C# WriteLine().
Code
using System;
using System.IO; // defining the System package
class Program2 {
public static void Main() //defining the main method
{
// declaring variables
int price;
string name;
int icecreams;
int total;
Console.WriteLine("Enter price of each ice-cream: ");
price = Convert.ToInt32(Console.ReadLine()); // Converting string to int
Console.WriteLine("Enter the customer's name: ");
name = Console.ReadLine(); //reading input
Console.WriteLine("Enter the number of icecreams that "+name+" has ordered");
icecreams = Convert.ToInt32(Console.ReadLine()); // Converting string to int
Total = icecreams * price; // calculating total price
// printing the final output
Console.WriteLine(name+" has ordered "+icecreams+
" ice-creams. The total amount to be paid is "+total);
}
}
Output
Example 3
Aim - Write a C# program to pause the console using the C# readline method.
File name - Program3.cs
The following program takes a random input from the user using C# ReadLine() and then prints it using C# WriteLine(). It follows this up by running a blank C# ReadLine() method, which pauses the console and will exit only if the user presses the Enter key.
Code
using System;
using System.IO; // defining the System package
class Program3 {
// defining the main function
public static void Main()
{
string random;
Console.WriteLine("Enter a random string: ");
random = Console.ReadLine(); // taking input from user
Console.WriteLine("The random string entered is : " +random);
// console paused until user presses enter key
Console.ReadLine();
}
}
Output
In the above output, the cursor will continue blinking until the user presses the enter key.
Conclusion
The C# readline method is one of the basic methods for taking in user input in C#.
It has several other uses, apart from taking in inputs like controlling and pausing the console to let the user read the output in case of large output data. It takes the complete string as input and then takes the cursor to the next line.Â
To master the power of C# readline and all its other methods, one might consider referring and learning in-depth from various resources, study materials, and course books.
If you are interested in learning more about C# in order to become a web and desktop application developer, Simplilearn offers an exclusive full-stack web development certification course to master both backend and frontend with tools, like SpringBoot, AngularMVC, JSPs, and Hibernate to start your career as a full-stack developer.
Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. Become a full-stack web developer today!