Console.ReadLine Method

Reads the next line of characters from the standard input stream.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Public Shared Function ReadLine As String
Dim returnValue As String

returnValue = Console.ReadLine
public static String ReadLine ()
public static function ReadLine () : String
Not applicable.

Return Value

The next line of characters from the input stream, or a null reference (Nothing in Visual Basic) if no more lines are available.

Exception typeCondition


An I/O error occurred.


There is insufficient memory to allocate a buffer for the returned string.


The number of characters in the next line of characters is greater than Int32.MaxValue.

A line is defined as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a), or the value of the Environment.NewLine property. The returned string does not contain the terminating character(s).

If this method throws OutOfMemoryException, the reader's position in the underlying Stream is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the TextReader. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.

To avoid such a situation and produce robust code you should use the Read method and store the read characters in a preallocated buffer.

The following code example illustrates the usage of the ReadLine method.

Public Class InsertTabs
   Private Const tabSize As Integer = 4
   Private Const usageText As String = "Usage: INSERTTABS inputfile.txt outputfile.txt"
   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
   End Sub
   Overloads Public Shared Function Main(args() As String) As Integer
      Dim writer As StreamWriter = Nothing
      If args.Length < 3 Then
         Return 1
      End If
         writer = New StreamWriter(args(2))
         Console.SetIn(New StreamReader(args(1)))
      Catch e As IOException
         Dim errorWriter As TextWriter = Console.Error
         Return 1
      End Try
      Dim line As String
      line = Console.ReadLine()
      While Not line Is Nothing
         Dim newLine As String = line.Replace("".PadRight(tabSize, " "c), ControlChars.Tab)
         line = Console.ReadLine()
      End While
      ' Recover the standard output stream so that a 
      ' completion message can be displayed.
      Dim standardOutput As New StreamWriter(Console.OpenStandardOutput())
      standardOutput.AutoFlush = True
      Console.WriteLine("INSERTTABS has completed the processing of {0}.", args(0))
      Return 0
   End Function 'Main
End Class 'InsertTabs

public class InsertTabs
    private static int tabSize = 4;
    private static String usageText = "Usage: INSERTTABS inputfile.txt"
        + " outputfile.txt";
    public static void main(String[] args)
        StreamWriter writer = null;

        if (args.length < 2) {        
            return ;

        try {        
            writer = new StreamWriter(args[1]);
            Console.SetIn(new StreamReader(args[0]));
        catch (IOException e) {        
            TextWriter errorWriter = Console.get_Error();
            return ;

        String line;
        while (((line = Console.ReadLine()) != null)) {        
            String newLine = line.Replace("".PadRight(tabSize, ' '), "\t");


        // Recover the standard output stream so that a 
        // completion message can be displayed.
        StreamWriter standardOutput = new StreamWriter(Console.
        Console.WriteLine("INSERTTABS has completed the processing of {0}.", 
    } //main
} //InsertTabs

const tabSize = 4;
const usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";

var writer : StreamWriter = null;
var args = Environment.GetCommandLineArgs();

if (args.Length != 3) {

try {
    writer = new StreamWriter(args[2]);
    Console.SetIn(new StreamReader(args[1]));
catch(e : IOException) {
    var errorWriter = Console.Error;
var line;
while ((line = Console.ReadLine()) != null) {
    var newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
// Recover the standard output stream so that a 
// completion message can be displayed.
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0