C# Console OpenStandardInput() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Console

Definition

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);
CallWhat 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

ParameterDescription
bufferSizeOptional. Suggested buffer size in bytes for the stream.

Return Value

Return ValueDescription
StreamA 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);
    }
}

How It Works

  1. Stream handle. OpenStandardInput returns a readable Stream.
  2. CanRead. Confirms the stream is open for reading.
  3. SetIn demo. Shows line input beside the raw stream object.
  4. 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);
    }
}

How It Works

  1. 1024-byte hint. Suggests a buffer to the stream implementation.
  2. using. Disposes the stream when finished.
  3. CanRead check. Quick sanity output for beginners.
  4. 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);
    }
}

How It Works

  1. Two layers. TextReader for characters; Stream for bytes.
  2. CanRead. Confirms the stdin stream is readable.
  3. Teaching output. Focuses on the idea, not deep internals.
  4. 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.

Next: OpenStandardOutput()

Return the raw standard output Stream (optionally buffered) with System.Console.

OpenStandardOutput() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful