C# Readline() Method

The Console.WriteLine is a method used in C# to print the entire statement of a single line and transfer control to the next line of the console. Similar to the Console.WriteLine, the ReadLine method is used to read the entire line of string or statement value from the user until the Enter key is pressed to transfer control to the next line. In this section, we will understand the ReadLine, Read and Readkey method in details.

Console.ReadLine Method

In C# , the ReadLine method is a commonly used method or function to take an input from the user until the enter key is pressed. In other words, it is a method that reads each line of string or values from a standard input stream.

It is a predefined method of the Console class (System Namespace). The Console.ReadLine method reads and only returns the string from the stream output device (console) until a newline character is found. If we want to read a character or numeric value from the user, we need to convert the string to the appropriate data sets.

Syntax

Example

public static string ReadLine ();

The method throws the following exceptions:

  • IOException: It occurs if an I/O error arises.
  • OutOfMemoryException: It occurs if there is insufficient memory to allocate a buffer to return a string.
  • ArgumentOutOfRangeException: It occurs if the number of characters in the next line that is greater than MaxValue .

Example 1: Let's write a program that takes an input from the user using the ReadLine method.

Program.cs

Example

using System; // Define the System package
namespace ConsoleApp3 // Project name or Folder
{
    class Program 
    {
        static void Main(string[] args) // Defining the main function
        {
            string name; // string variable name
            Console.WriteLine("Hello, what is your name?"); 
            name = Console.ReadLine(); // takes an input from the user
            Console.WriteLine("Hi! "+ name + " Welcome to the C# Tutorial"); // print the output
        }
    }
}

Output

Example 2: Write a program to print the user's first and last name using the ReadLine function in C#.

Program2.cs

Example

using System;

namespace ConsoleApp3
{
    class Program2
    {
        static void Main(string[] args)
        {
            string fname, lname; // string variables
            Console.Write("Please, Enter your first Name : ");
            fname = Console.ReadLine(); // takes the first name from the user

            // ReadLine() is a method of Console class to read a line from the standard input stream
            Console.Write("Please, Enter Your Last Name : ");
            lname = Console.ReadLine(); // takes the second name from the user

            Console.WriteLine("Your Full Name is : " + fname + " " + lname);
        }
    }
}

Output

Read Method

The Read method in C# is used to read a single character from the user. It is different from the ReadLine method because the readLine method receives each line input from the user until the line is finished and control transfer to the next statement to read strings.

Let's write a program to understand the use of the Read method in C# to print a character.

Program4.cs

Example

using System; // Define the System package
namespace ConsoleApp3
{
    class Program4 
    {
        static void Main(string[] args) 
        {
            char ch;
            Console.Write("Enter the characters "); // Cosole.Write() print the same line statement.
            ch = Convert.ToChar(Console.Read()); // Read a single character from the user.
            Console.WriteLine("You have entered the character " + ch); //print the complete line
        }
    }
}

Output

ReadKey

The ReadKey method is used to get the next character, or the user presses any key to exit the program. It holds the screen until the user presses any key from the keyboard. The pressed key will be displayed on the console.

Program5.cs

Example

using System; // Define the System package

namespace ConsoleApp3
{
    class Program5 
    {
        static void Main(string[] args) 
        {     
             DateTime dt = DateTime.Now; // DateTime.Now() print the current time
             Console.WriteLine(" The Current Date and Time is : " + dt);
             Console.Write("Press any key or Enter to exit from the Console Screen");
             Console.ReadKey(); // enter any key to exit from the console screen.
            }
    }
}

Output

Input Required

This code uses input(). Please provide values below: