In C#, Console.OpenStandardInput returns a Stream connected to the process’s standard input. That is a lower-level handle than Console.In (a TextReader).
Use it when you must read raw bytes, wrap your own StreamReader, or pass stdin to APIs that require a Stream. An overload accepts a buffer size.
This is a static member of System.Console. You call it with the type name, such as Console.OpenStandardInput(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.
Syntax
Open the stream, wrap a reader if you want text, and dispose when done.
C#
using Stream stdin = Console.OpenStandardInput();
using var reader = new StreamReader(stdin);
Call
What it does
Console.OpenStandardInput()
Returns the standard input Stream.
Console.OpenStandardInput(4096)
Same, with a suggested buffer size.
SetIn replaces the TextReader Console uses; OpenStandardInput gives the underlying stdin Stream.
Library declarations
C#
public static Stream OpenStandardInput();
public static Stream OpenStandardInput(int bufferSize);
Parameters
Parameter
Description
bufferSize
Optional. Suggested buffer size in bytes for the stream.
Return Value
Return Value
Description
Stream
A stream for standard input.
C# Examples
Example 1 — Build a StreamReader on Stdin
Open stdin and read one line through a StreamReader.
C#
using System;
using System.IO;
class Program {
static void Main() {
Console.SetIn(new StringReader("sample"));
using Stream stdin = Console.OpenStandardInput();
// For a deterministic demo, read via Console after SetIn instead:
Console.WriteLine("via ReadLine: " + Console.ReadLine());
Console.WriteLine("stdin CanRead=" + stdin.CanRead);
}
}
Output
via ReadLine: sample
stdin CanRead=True
How It Works
Stream handle. OpenStandardInput returns a readable Stream.
CanRead. Confirms the stream is open for reading.
SetIn demo. Shows line input beside the raw stream object.
Prefer ReadLine. For text lines, the Console helpers stay simpler.
Example 2 — Request a Buffer When Opening
Pass a buffer size to the overload.
C#
using System;
using System.IO;
class Program {
static void Main() {
using Stream stdin = Console.OpenStandardInput(1024);
Console.WriteLine("Opened stdin, CanRead=" + stdin.CanRead);
}
}
Output
Opened stdin, CanRead=True
How It Works
1024-byte hint. Suggests a buffer to the stream implementation.
using. Disposes the stream when finished.
CanRead check. Quick sanity output for beginners.
Byte-oriented. Still a Stream, not a TextReader.
Example 3 — TextReader vs Stream
Remind when to use each API.
C#
using System;
using System.IO;
class Program {
static void Main() {
Console.WriteLine("ReadLine uses Console.In (TextReader)");
Console.WriteLine("OpenStandardInput returns a Stream");
using Stream s = Console.OpenStandardInput();
Console.WriteLine("CanRead=" + s.CanRead);
}
}
Output
ReadLine uses Console.In (TextReader)
OpenStandardInput returns a Stream
CanRead=True
How It Works
Two layers. TextReader for characters; Stream for bytes.
CanRead. Confirms the stdin stream is readable.
Teaching output. Focuses on the idea, not deep internals.
Choose wisely. Beginners usually want ReadLine first.
Frequently Asked Questions
A Stream for the process standard input.
No. In is a TextReader. OpenStandardInput is the lower-level Stream.
When an API requires Stream, or you need raw bytes.
An optional hint for buffering when opening the stream.
OpenStandardOutput and OpenStandardError for the other standard streams.