.NET API Documentation

System.Console.ReadLine Method

public static string ReadLine()

Reads the next line of characters from the standard input stream and returns them as a string.

The returned string does not contain the terminating newline character.

Parameters

This method does not take any parameters.

Return Value

string
The next line from the input stream, or null if the end of the stream has been reached.

Exceptions

  • IOException
    An I/O error occurred.
  • OutOfMemoryException
    There is not enough memory available to host the returned string.

Remarks

The ReadLine method reads characters from the standard input stream until a newline character is encountered. The newline character is read but not included in the returned string.

If the end of the input stream is reached before a newline character is encountered, the method returns the characters read up to that point.

The ReadLine method can be used to read a single line of text from the console, a file, or any other input stream.

Example

// This example demonstrates how to use the Console.ReadLine method. // It prompts the user for their name and then greets them. using System; public class Example { public static void Main() { Console.Write("Enter your name: "); string name = Console.ReadLine(); if (name != null) { Console.WriteLine($"Hello, {name}!"); } else { Console.WriteLine("No input received."); } } }

See Also