C# Readline() Method

The Console.WriteLine function is commonly employed in C# for displaying an entire statement on a single line and moving the cursor to the next line in the console. Much like the Console.WriteLine, the ReadLine function is utilized to capture an entire line of text or statement input from the user until the Enter key is pressed to move to the next line. In the upcoming section, we will delve into the specifics of the ReadLine, Read and Readkey functions.

The <style> element is represented by the placeholder diagram below:

Example

<div class="placeholder-diagram">
  <i class="placeholder-icon">icon</i>
  <p class="placeholder-text">Description of the element</p>
</div>

This diagram showcases the styling for the </style>.

Console.ReadLine Method

In C# programming language, the ReadLine method is frequently employed to receive user input until the Enter key is pressed. Essentially, it functions by reading individual lines of text or data from the standard input stream.

It is a predetermined function within the Console class (System Namespace). The Console.ReadLine function is responsible for capturing and retrieving the text input from the output stream, which in this case is the console, until a line break is encountered. To obtain a character or numerical input from the user, we must transform the received text into the relevant data types.

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: We will create a program that prompts the user for input by utilizing 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

The CSS code snippet below demonstrates the styling for a placeholder diagram:

Example

.placeholder-diagram { background: linear-gradient(135deg, #374151 0%, #1f2937 100%); border-radius: 12px; padding: 40px; margin: 20px 0; text-align: center; }
.placeholder-diagram .placeholder-icon { font-size: 3rem; margin-bottom: 10px; }
.placeholder-diagram .placeholder-text { color: #9ca3af; font-size: 1rem; }

Develop a C# program that utilizes the ReadLine function to display the user's given first and last name.

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

The <style> component's style is defined by the following CSS properties:

Example

.placeholder-diagram { 
  background: linear-gradient(135deg, #374151 0%, #1f2937 100%); 
  border-radius: 12px; 
  padding: 40px; 
  margin: 20px 0; 
  text-align: center; 
}

.placeholder-diagram .placeholder-icon { 
  font-size: 3rem; 
  margin-bottom: 10px; 
}

.placeholder-diagram .placeholder-text { 
  color: #9ca3af; 
  font-size: 1rem; 
}

These properties collectively define the visual appearance of the </style> element.

Read Method

The Read function in C# is employed to retrieve a solitary character from the user. It distinguishes itself from the ReadLine function as the ReadLine function captures each line of input from the user until the line is completed, allowing the control to move on to the subsequent statement for string reading.

Let's develop a program in C# to explore how the Read function is employed to display a single 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

The styling for the placeholder is defined in the following CSS code snippet:

.placeholder-diagram {

background: linear-gradient(135deg, #374151 0%, #1f2937 100%);

border-radius: 12px;

padding: 40px;

margin: 20px 0;

text-align: center;

}

.placeholder-diagram .placeholder-icon {

font-size: 3rem;

margin-bottom: 10px;

}

.placeholder-diagram .placeholder-text {

color: #9ca3af;

font-size: 1rem;

}

ReadKey

The ReadKey function is employed to retrieve the subsequent character, or the program exits when any key is pressed by the user. It pauses the display until a key is pressed on the keyboard. The key that is pressed will then appear 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: