In C#, Console.OpenStandardOutput returns a Stream for standard output. It is the byte-level companion to Console.Out (a TextWriter).
A frequent pattern after capturing output with SetOut is to wrap OpenStandardOutput in a StreamWriter to talk to the real console again.
This is a static member of System.Console. You call it with the type name, such as Console.OpenStandardOutput(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.
Syntax
Open the stream and wrap a StreamWriter when you need text.
SetOut changes the TextWriter; OpenStandardOutput returns the underlying stdout Stream.
Library declarations
C#
public static Stream OpenStandardOutput();
public static Stream OpenStandardOutput(int bufferSize);
Parameters
Parameter
Description
bufferSize
Optional. Suggested buffer size in bytes.
Return Value
Return Value
Description
Stream
A stream for standard output.
C# Examples
Example 1 — Restore Console.Out After Capture
Capture with StringWriter, then reconnect to stdout.
C#
using System;
using System.IO;
class Program {
static void Main() {
var sw = new StringWriter();
Console.SetOut(sw);
Console.WriteLine("hidden-from-screen");
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
Console.WriteLine("back on console");
Console.WriteLine("captured=" + sw.ToString().Trim());
}
}
Output
back on console
captured=hidden-from-screen
How It Works
Capture first. WriteLine goes into the StringWriter.
OpenStandardOutput. Gets the real stdout Stream.
New StreamWriter. Becomes Console.Out again.
Show both. Live line plus the captured text.
Example 2 — Check That Stdout Is Writable
Open the stream and print CanWrite.
C#
using System;
using System.IO;
class Program {
static void Main() {
using Stream stdout = Console.OpenStandardOutput();
Console.WriteLine("CanWrite=" + stdout.CanWrite);
}
}
Output
CanWrite=True
How It Works
Writable stream. Stdout should allow writing.
using dispose. Releases the stream wrapper when done.
Simple probe. Good first check for beginners.
Text still via Out. This WriteLine uses the TextWriter path.
Example 3 — Open Stdout With a Buffer Size
Use the overload that takes a buffer size.
C#
using System;
using System.IO;
class Program {
static void Main() {
using Stream stdout = Console.OpenStandardOutput(2048);
Console.WriteLine("stdout opened with buffer hint");
Console.WriteLine("CanWrite=" + stdout.CanWrite);
}
}
Output
stdout opened with buffer hint
CanWrite=True
How It Works
2048 hint. Suggests buffering to the implementation.
Still a Stream. Byte-oriented API under the hood.
Two lines. Confirm open success with a CanWrite check.
Pair with SetOut. Often used when rebuilding writers.
Frequently Asked Questions
A Stream connected to standard output.
So you can SetOut back to a text writer attached to the real console.
No. WriteLine already uses Console.Out.
Open returns the stream; SetOut replaces the TextWriter Console uses.