Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Console.Read Method

Reads the next character from the standard input stream.

[Visual Basic]
Public Shared Function Read() As Integer
[C#]
public static int Read();
[C++]
public: static int Read();
[JScript]
public static function Read() : int;

Return Value

The next character from the input stream, or negative one (-1) if no more characters are available.

Exceptions

Exception Type Condition
IOException An I/O error occurred.

Remarks

This method will not return until the read operation is terminated; for example, by the user pressing the enter key. If data is available, the input stream contains what the user entered, suffixed with the environment dependent newline character.

Example

[Visual Basic] 
Dim i As Integer
Dim c As Char
While True
    i = Console.Read()
    If i = - 1 Then
        Exit While
    End If
    c = Microsoft.VisualBasic.Chr(i)
    Console.WriteLine("Echo: {0}", c)
End While
Console.WriteLine("Done")
Return 0

[C#] 
int i;
char c;
while (true)
{
 i = Console.Read ();
 if (i == -1) break;
 c = (char) i;
 Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");
return 0;

[C++] 
int i;
char c;
for(;;)
{
 i = Console::Read ();
 if (i == -1) break;
 c = (char) i;
 Console::WriteLine (S"Echo: {0}", __box(c));
}
Console::WriteLine (S"Done");
return 0;

[JScript] 
var i : int;
var c : char;
while (true)
{
 i = Console.Read ();
 if (i == -1) break;
 c = char(i);
 Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");
return 0;

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Console Class | Console Members | System Namespace | ReadLine | Write | WriteLine

Show:
© 2015 Microsoft