C# Console SetOut() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Console

Definition

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

ParameterDescription
newOutRequired. The TextWriter that becomes standard output.

Return Value

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

How It Works

  1. Save Out. Keep the original writer to restore later.
  2. SetOut(sw). WriteLine fills the StringWriter.
  3. Restore. Point Console back at the real console.
  4. 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);
    }
}

How It Works

  1. Five letters. Write twice builds abcde with no newline.
  2. OpenStandardOutput. Helps rebuild a console writer after redirect.
  3. chars=5. Length matches the captured text exactly.
  4. 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");
    }
}

How It Works

  1. Null writer. Discards everything written.
  2. Hidden line. Does not appear in sample output.
  3. Restore Out. Later messages show again.
  4. 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.
No. Input uses SetIn.

Next: SetWindowPosition()

Move which part of the buffer is visible (window origin) with System.Console.

SetWindowPosition() 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.

12 people found this page helpful