C# Console SetIn() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Console

Definition

In C#, Console.SetIn replaces the TextReader behind standard input. Afterward, ReadLine, Read, and related calls read from your reader instead of the keyboard.

Common choices are StringReader for tests and StreamReader for files. Restore the original reader if the rest of the program still needs the keyboard.

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

Syntax

Pass any TextReader. Remember to dispose file readers when finished.

C#
Console.SetIn(new StringReader("Ada\\n"));
string name = Console.ReadLine();
CallWhat it does
Console.SetIn(reader)Makes Console input methods use that TextReader.

SetOut and SetError redirect output streams the same way.

Library declarations
C#
public static void SetIn(TextReader newIn);

Parameters

ParameterDescription
newInRequired. The TextReader that becomes standard input.

Return Value

Return ValueDescription
voidInput is redirected; nothing is returned.

C# Examples

Example 1 — Feed ReadLine From a String

Simulate typed input without a keyboard.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.SetIn(new StringReader("Ada"));
        string? name = Console.ReadLine();
        Console.WriteLine("Hello, " + name);
    }
}

How It Works

  1. Fake keyboard. StringReader supplies the line Ada.
  2. SetIn. Points Console input at that reader.
  3. ReadLine works. Same API as interactive input.
  4. Great for demos. Output stays deterministic.

Example 2 — Provide Multiple Lines of Input

Stack lines with newlines inside the string.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.SetIn(new StringReader("3\\n4\\n"));
        int a = int.Parse(Console.ReadLine()!);
        int b = int.Parse(Console.ReadLine()!);
        Console.WriteLine("sum=" + (a + b));
    }
}

How It Works

  1. Two ReadLine calls. Each consumes one line from the reader.
  2. Parse ints. Convert text lines to numbers.
  3. sum 7. 3 + 4 printed after both lines are read.
  4. Deterministic test. No human typing required.

Example 3 — See null When the Reader Ends

Call ReadLine after the content is exhausted.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.SetIn(new StringReader("only"));
        Console.WriteLine(Console.ReadLine());
        string? again = Console.ReadLine();
        Console.WriteLine(again == null ? "EOF null" : again);
    }
}

How It Works

  1. First line. Returns only.
  2. Second call. Reader is empty β†’ null.
  3. Null means EOF. Same rule as end of redirected input.
  4. Check before use. Do not assume another string arrives.

Frequently Asked Questions

The TextReader used by Console.Read, ReadLine, and In.
Yes. Pass a StreamReader opened on the file.
StringReader lets tests supply input without a keyboard.
No. Input and output redirects are separate.
ReadLine returns null; Read returns -1.

Next: SetOut()

Redirect Console standard output to a different TextWriter with System.Console.

SetOut() 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