This documentation is archived and is not being maintained.

Console.Read Method

Reads the next character from the standard input stream.

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

<HostProtectionAttribute(SecurityAction.LinkDemand, UI := True)> _
Public Shared Function Read As Integer
Dim returnValue As Integer 

returnValue = Console.Read()

Return Value

Type: System.Int32
The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.


An I/O error occurred.


The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: UI. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.

Note that you will not get a property value of -1 unless you perform one of the following actions: simultaneously press the Control modifier key and Z console key (CTRL+Z), which signals the end-of-file condition; press an equivalent key that signals the end-of-file condition, such as the F6 function key in Windows; or redirect the input stream to a source, such as a text file, that has an actual end-of-file character.

The ReadLine method, or the KeyAvailable property and ReadKey method are preferable to using the Read method.

The following code example demonstrates the Read method.

' This example demonstrates the Console.Read() method. 
Imports System
Imports Microsoft.VisualBasic

Class Sample
   Public Shared Sub Main()
      Dim m1 As String = _
                vbCrLf & _
                "Type a string of text then press Enter. " & _
                "Type '+' anywhere in the text to quit:" & _
      Dim m2 As String = "Character '{0}' is hexadecimal 0x{1:x4}." 
      Dim m3 As String = "Character     is hexadecimal 0x{0:x4}." 
      Dim ch As Char 
      Dim x As Integer 
         x = Console.Read()
            ch = Convert.ToChar(x)
            If Char.IsWhiteSpace(ch) Then
               Console.WriteLine(m3, x)
               If ch = vbLf Then
               End If 
               Console.WriteLine(m2, ch, x)
            End If 
         Catch e As OverflowException
            Console.WriteLine("{0} Value read = {1}.", e.Message, x)
            ch = Char.MinValue
         End Try 
      Loop While ch <> "+"c
   End Sub 'Main
End Class 'Sample

'This example produces the following results: 

'Type a string of text then press Enter. Type '+' anywhere in the text to quit: 

'The quick brown fox. 
'Character 'T' is hexadecimal 0x0054. 
'Character 'h' is hexadecimal 0x0068. 
'Character 'e' is hexadecimal 0x0065. 
'Character     is hexadecimal 0x0020. 
'Character 'q' is hexadecimal 0x0071. 
'Character 'u' is hexadecimal 0x0075. 
'Character 'i' is hexadecimal 0x0069. 
'Character 'c' is hexadecimal 0x0063. 
'Character 'k' is hexadecimal 0x006b. 
'Character     is hexadecimal 0x0020. 
'Character 'b' is hexadecimal 0x0062. 
'Character 'r' is hexadecimal 0x0072. 
'Character 'o' is hexadecimal 0x006f. 
'Character 'w' is hexadecimal 0x0077. 
'Character 'n' is hexadecimal 0x006e. 
'Character     is hexadecimal 0x0020. 
'Character 'f' is hexadecimal 0x0066. 
'Character 'o' is hexadecimal 0x006f. 
'Character 'x' is hexadecimal 0x0078. 
'Character '.' is hexadecimal 0x002e. 
'Character     is hexadecimal 0x000d. 
'Character     is hexadecimal 0x000a. 

'Type a string of text then press Enter. Type '+' anywhere in the text to quit: 

'Value was either too large or too small for a character. Value read = -1. 

'Type a string of text then press Enter. Type '+' anywhere in the text to quit: 

'Character '+' is hexadecimal 0x002b. 

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0