Read Method

Console.Read Method ()

 

Reads the next character from the standard input stream.

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

[<HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)>]
static member Read : unit -> int

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.

Exception Condition
IOException

An I/O error occurred.

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.

System_CAPS_importantImportant

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

Note that the method does not return -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.

  • Redirect the input stream to a source, such as a text file, that has an actual end-of-file character.

The following example demonstrates the Read method.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft