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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

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());

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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  

        }  

    }

 

Image Reference 

Output  

C_Sharp_readline_1.

C_Sharp_readline_2

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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); 

}

}

Image Reference

Output

C_Sharp_readline_3.
C_Sharp_readline_4.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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();

}

}

Image Reference

Output

C_Sharp_readline_5.

C_Sharp_readline_6

In the above output, the cursor will continue blinking until the user presses the enter key.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 24 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449