In C#, Console.Read pulls the next character from standard input and returns it as an int (the Unicode code point). When there is no more input, it returns -1.
It is lower-level than ReadLine. You often cast the result to char after checking that it is not -1. For whole lines of text, prefer ReadLine; for a single key press with details, prefer ReadKey.
This is a static member of System.Console. You call it with the type name, such as Console.Read(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.
Syntax
Call Console.Read(), then test for -1 before casting to char.
C#
int code = Console.Read();
if (code != -1) {
char ch = (char)code;
}
Call
What it does
Console.Read()
Next character as int, or -1 at EOF.
Input is usually line-buffered in a terminal: the user may need to press Enter before characters become available.
Library declarations
C#
public static int Read();
Parameters
Parameter
Description
None
Read() takes no parameters.
Return Value
Return Value
Description
int (0–…)
The next character’s code point.
-1
End of input (EOF).
C# Examples
Example 1 — Read a Character After Enter
Ask for a letter, read one character with Read, then show its code.
C#
using System;
class Program {
static void Main() {
Console.Write("Type a letter and press Enter: ");
int code = Console.Read();
if (code == -1) {
Console.WriteLine("No input");
return;
}
char ch = (char)code;
Console.WriteLine();
Console.WriteLine("You typed: " + ch);
Console.WriteLine("Code: " + code);
}
}
Output
Type a letter and press Enter: A
You typed: A
Code: 65
How It Works
Prompt first.Write keeps the cursor on the same line as the question.
Sample typing. Output shows the user typing A and Enter.
Int then cast.65 is the code for 'A'; cast only after the EOF check.
Extra newline. An empty WriteLine keeps the report tidy after the Enter key.
Example 2 — Count Characters Until End
Loop while Read returns values other than -1.
C#
using System;
class Program {
static void Main() {
Console.WriteLine("Type text, then Ctrl+Z (Windows) or Ctrl+D and Enter:");
int count = 0;
int code;
while ((code = Console.Read()) != -1) {
count++;
}
Console.WriteLine("Characters read: " + count);
}
}
Output
Type text, then Ctrl+Z (Windows) or Ctrl+D and Enter:
hi
Characters read: 3
How It Works
Loop condition. Assignment inside while keeps reading until EOF.
Sample stream. Typing hi plus Enter typically yields three codes (h, i, newline).
-1 stops. End-of-file ends the loop cleanly.
Count only. This demo tallies characters; it does not echo them.
Example 3 — Test Whether the First Char Is a Digit
Read one character and classify it with char.IsDigit.
C#
using System;
class Program {
static void Main() {
Console.Write("First character: ");
int code = Console.Read();
if (code < 0) {
Console.WriteLine("EOF");
return;
}
char ch = (char)code;
Console.WriteLine();
Console.WriteLine(char.IsDigit(ch) ? "Digit" : "Not a digit");
}
}
Output
First character: 7
Digit
How It Works
Guard EOF. Negative means no character arrived.
Typed 7. sample output shows a digit being entered.
IsDigit. Classifies the character without parsing a whole number string.
Read vs ReadLine. Here only the first character matters, so Read fits.
Frequently Asked Questions
The next character as an int code point, or -1 when input ends.
Most consoles buffer a line. Characters often become available after Enter.
Read returns one character as int. ReadLine returns a whole string line (or null at EOF).
ReadKey returns a ConsoleKeyInfo for a key press and can hide the echo. Read is stream-oriented.
Ensure the return value is not -1, then cast to char.