How to get the Standard Input and Output Stream through Console in C#?

The Console class in C# provides three standard streams for input and output operations: Console.In for standard input, Console.Out for standard output, and Console.Error for standard error. These streams allow you to access the underlying TextReader and TextWriter objects used by the console.

Standard Streams Overview

The console streams are represented as properties that return the following types −

  • Console.In − Returns a TextReader object for reading standard input
  • Console.Out − Returns a TextWriter object for writing to standard output
  • Console.Error − Returns a TextWriter object for writing to standard error

Console Streams Console.In TextReader Input Stream Console.Out TextWriter Output Stream Console.Error TextWriter Error Stream Keyboard Screen Error Output Standard I/O streams redirect to appropriate devices

Accessing Standard Input Stream

Example

using System;

public class Demo {
    public static void Main(string[] args) {
        Console.WriteLine("Displaying standard input stream...");
        Console.WriteLine("Standard Input Stream = " + Console.In);
        Console.WriteLine("Input stream type: " + Console.In.GetType().Name);
    }
}

The output of the above code is −

Displaying standard input stream...
Standard Input Stream = System.IO.TextReader+SyncTextReader
Input stream type: SyncTextReader

Accessing Standard Output Stream

Example

using System;

public class Demo {
    public static void Main(string[] args) {
        Console.WriteLine("Displaying standard output stream...");
        Console.WriteLine("Standard Output Stream = " + Console.Out);
        Console.WriteLine("Output stream type: " + Console.Out.GetType().Name);
    }
}

The output of the above code is −

Displaying standard output stream...
Standard Output Stream = System.IO.TextWriter+SyncTextWriter
Output stream type: SyncTextWriter

Accessing All Console Streams

Example

using System;

public class Demo {
    public static void Main(string[] args) {
        Console.WriteLine("Console Stream Information:");
        Console.WriteLine("==========================");
        Console.WriteLine("Standard Input:  " + Console.In);
        Console.WriteLine("Standard Output: " + Console.Out);
        Console.WriteLine("Standard Error:  " + Console.Error);
        
        Console.WriteLine("\nStream Types:");
        Console.WriteLine("Input Type:  " + Console.In.GetType().FullName);
        Console.WriteLine("Output Type: " + Console.Out.GetType().FullName);
        Console.WriteLine("Error Type:  " + Console.Error.GetType().FullName);
    }
}

The output of the above code is −

Console Stream Information:
==========================
Standard Input:  System.IO.TextReader+SyncTextReader
Standard Output: System.IO.TextWriter+SyncTextWriter
Standard Error:  System.IO.TextWriter+SyncTextWriter

Stream Types:
Input Type:  System.IO.TextReader+SyncTextReader
Output Type: System.IO.TextWriter+SyncTextWriter
Error Type:  System.IO.TextWriter+SyncTextWriter

Using Console Streams for Custom Operations

Example

using System;
using System.IO;

public class Demo {
    public static void Main(string[] args) {
        TextWriter output = Console.Out;
        TextWriter error = Console.Error;
        
        output.WriteLine("This goes to standard output");
        error.WriteLine("This goes to standard error");
        
        // Using WriteLine method directly
        Console.Out.WriteLine("Direct output stream usage");
        Console.Error.WriteLine("Direct error stream usage");
        
        output.Flush();
        error.Flush();
    }
}

The output of the above code is −

This goes to standard output
This goes to standard error
Direct output stream usage
Direct error stream usage

Conclusion

Console streams in C# provide access to the underlying TextReader and TextWriter objects for standard input, output, and error. The Console.In, Console.Out, and Console.Error properties return synchronized stream wrappers that ensure thread-safe console operations.

Updated on: 2026-03-17T07:04:36+05:30

488 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements