C# Console SetError() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Console

Definition

In C#, Console.SetError replaces the TextWriter for standard error. Messages written with Console.Error.WriteLine go to that writer.

Keeping errors separate from normal output helps logs and pipelines. Pair with SetOut when you need both streams redirected differently.

This is a static member of System.Console. You call it with the type name, such as Console.SetError(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.

Syntax

Pass a TextWriter, then write through Console.Error.

C#
Console.SetError(new StringWriter());
Console.Error.WriteLine("oops");
CallWhat it does
Console.SetError(writer)Sends error writes to that TextWriter.

OpenStandardError returns the raw stderr Stream if you need a lower-level handle.

Library declarations
C#
public static void SetError(TextWriter newError);

Parameters

ParameterDescription
newErrorRequired. The TextWriter that becomes standard error.

Return Value

Return ValueDescription
voidError output is redirected.

C# Examples

Example 1 — Capture Console.Error Into a String

Redirect stderr, write an error, then show the capture on stdout.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        var err = new StringWriter();
        var original = Console.Error;
        Console.SetError(err);
        Console.Error.WriteLine("file missing");
        Console.SetError(original);
        Console.WriteLine("captured: " + err.ToString().Trim());
    }
}

How It Works

  1. Separate stream. Error text does not go through Out while redirected.
  2. StringWriter. Holds the diagnostic in memory.
  3. Restore Error. Put the original writer back.
  4. Report on Out. Print a summary with WriteLine.

Example 2 — Write Normal and Error Messages

Show that Out and Error are independent.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine("status: ok");
        Console.Error.WriteLine("diag: check logs");
    }
}

How It Works

  1. WriteLine → Out. Normal status message.
  2. Error.WriteLine. Diagnostic on stderr.
  3. Both visible. Many terminals show them together.
  4. Pipelines differ. Shells can split the streams.

Example 3 — Send Errors to TextWriter.Null

Silence diagnostics temporarily.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        var original = Console.Error;
        Console.SetError(TextWriter.Null);
        Console.Error.WriteLine("hidden");
        Console.SetError(original);
        Console.WriteLine("done");
    }
}

How It Works

  1. Null error writer. Discards Error writes.
  2. Hidden diag. Does not appear in output.
  3. Restore. Later errors can show again.
  4. done on Out. Normal output still works.

Frequently Asked Questions

For diagnostics and failures you want on stderr, separate from normal program output.
No. WriteLine uses Out. Use Console.Error.WriteLine for the error writer.
Yes. SetError to a StringWriter and read ToString().
SetOut, SetIn, and OpenStandardError.
No. It only changes where error text is written.

Next: SetIn()

Redirect Console standard input to a different TextReader with System.Console.

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

10 people found this page helpful