In C#, Console.SetOut replaces the TextWriter for standard output. Afterward, Write and WriteLine go to your writer instead of the screen.
Use StringWriter to capture text in memory, or a StreamWriter to log to a file. Call Flush when you need the data immediately, and restore the original writer if needed.
This is a static member of System.Console. You call it with the type name, such as Console.SetOut(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.
Syntax
Pass a TextWriter, write as usual, then read from the writer.
C#
var sw = new StringWriter();
Console.SetOut(sw);
Console.WriteLine("Hi");
string text = sw.ToString();
Call
What it does
Console.SetOut(writer)
Sends Write/WriteLine to that TextWriter.
Error output uses SetError separately. OpenStandardOutput returns the raw stdout Stream.
Library declarations
C#
public static void SetOut(TextWriter newOut);
Parameters
Parameter
Description
newOut
Required. The TextWriter that becomes standard output.
Return Value
Return Value
Description
void
Output is redirected; nothing is returned.
C# Examples
Example 1 — Capture WriteLine Into a String
Redirect to StringWriter and print the captured text afterward.
C#
using System;
using System.IO;
class Program {
static void Main() {
var sw = new StringWriter();
var original = Console.Out;
Console.SetOut(sw);
Console.WriteLine("Hello");
Console.WriteLine("World");
Console.SetOut(original);
Console.Write(sw.ToString());
}
}
Output
Hello
World
How It Works
Save Out. Keep the original writer to restore later.
SetOut(sw). WriteLine fills the StringWriter.
Restore. Point Console back at the real console.
Dump capture. sw.ToString() holds both lines.
Example 2 — Redirect Then Measure Captured Text
Capture output and report how many characters were written.
C#
using System;
using System.IO;
class Program {
static void Main() {
var sw = new StringWriter();
Console.SetOut(sw);
Console.Write("abc");
Console.Write("de");
string captured = sw.ToString();
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
Console.WriteLine("chars=" + captured.Length);
}
}
Output
chars=5
How It Works
Five letters.Write twice builds abcde with no newline.
OpenStandardOutput. Helps rebuild a console writer after redirect.
chars=5. Length matches the captured text exactly.
Testing aid. Measure or assert on captured strings.
Example 3 — Send Output to TextWriter.Null
Discard writes, then restore.
C#
using System;
using System.IO;
class Program {
static void Main() {
var original = Console.Out;
Console.SetOut(TextWriter.Null);
Console.WriteLine("You will not see this");
Console.SetOut(original);
Console.WriteLine("Visible again");
}
}
Output
Visible again
How It Works
Null writer. Discards everything written.
Hidden line. Does not appear in sample output.
Restore Out. Later messages show again.
Quiet sections. Useful when calling noisy helpers in demos.
Frequently Asked Questions
The TextWriter used by Console.Write, WriteLine, and Out.
SetOut to a StringWriter, run code, then read ToString().
Similar idea, but for the error writer used by Error.WriteLine.
Keep Console.Out before redirecting, then SetOut(original), or wrap OpenStandardOutput.