How to change the Output Encoding Scheme of the C# Console?

The Console.OutputEncoding property in C# allows you to change the character encoding scheme used for console output. This is particularly useful when working with non-ASCII characters, special symbols, or when you need to ensure compatibility with specific encoding standards.

By default, the console uses the system's default encoding, but you can override this behavior using different encoding schemes like ASCII, UTF-8, UTF-16, or Unicode.

Syntax

Following is the syntax for setting the output encoding −

Console.OutputEncoding = Encoding.EncodingType;

To get the current output encoding −

Encoding currentEncoding = Console.OutputEncoding;

Using ASCII Encoding

ASCII encoding supports only basic English characters (0-127). Here's how to set and display the output encoding −

using System;
using System.Text;

class Demo {
    public static void Main(string[] args) {
        Console.BackgroundColor = ConsoleColor.Black;
        Console.WriteLine("Background color changed = " + Console.BackgroundColor);
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("\nForeground color changed = " + Console.ForegroundColor);
        Console.InputEncoding = Encoding.ASCII;
        Console.WriteLine("Input Encoding Scheme = " + Console.InputEncoding);
        Console.OutputEncoding = Encoding.ASCII;
        Console.WriteLine("Output Encoding Scheme = " + Console.OutputEncoding);
    }
}

The output of the above code is −

Background color changed = Black

Foreground color changed = White
Input Encoding Scheme = System.Text.ASCIIEncoding
Output Encoding Scheme = System.Text.ASCIIEncoding

Using UTF-8 Encoding

UTF-8 encoding supports a much wider range of characters including international characters and symbols −

using System;
using System.Text;

class EncodingDemo {
    public static void Main(string[] args) {
        Console.WriteLine("Default Encoding: " + Console.OutputEncoding);
        
        Console.OutputEncoding = Encoding.UTF8;
        Console.WriteLine("New Encoding: " + Console.OutputEncoding);
        
        Console.WriteLine("Special characters: ñ, é, ü, ?, ©");
        Console.WriteLine("Math symbols: ?, ?, ?, ?");
    }
}

The output of the above code is −

Default Encoding: System.Text.UTF8Encoding
New Encoding: System.Text.UTF8Encoding
Special characters: ñ, é, ü, ?, ©
Math symbols: ?, ?, ?, ?

Comparing Different Encodings

Here's an example that demonstrates the difference between various encoding schemes −

using System;
using System.Text;

class EncodingComparison {
    public static void Main(string[] args) {
        string testText = "Hello World - Special: é, ñ, ü";
        
        Console.WriteLine("Original text: " + testText);
        Console.WriteLine();
        
        // ASCII Encoding
        Console.OutputEncoding = Encoding.ASCII;
        Console.WriteLine("ASCII Encoding: " + Console.OutputEncoding.EncodingName);
        Console.WriteLine("Code Page: " + Console.OutputEncoding.CodePage);
        
        // UTF-8 Encoding
        Console.OutputEncoding = Encoding.UTF8;
        Console.WriteLine("\nUTF-8 Encoding: " + Console.OutputEncoding.EncodingName);
        Console.WriteLine("Code Page: " + Console.OutputEncoding.CodePage);
        Console.WriteLine("Test: " + testText);
        
        // Unicode Encoding
        Console.OutputEncoding = Encoding.Unicode;
        Console.WriteLine("\nUnicode Encoding: " + Console.OutputEncoding.EncodingName);
        Console.WriteLine("Code Page: " + Console.OutputEncoding.CodePage);
    }
}

The output of the above code is −

Original text: Hello World - Special: é, ñ, ü

ASCII Encoding: US-ASCII
Code Page: 20127

UTF-8 Encoding: Unicode (UTF-8)
Code Page: 65001
Test: Hello World - Special: é, ñ, ü

Unicode Encoding: Unicode
Code Page: 1200

Common Encoding Types

Encoding Type Description Use Case
Encoding.ASCII 7-bit encoding, supports characters 0-127 Basic English text only
Encoding.UTF8 Variable-width encoding, supports all Unicode characters International text, web applications
Encoding.Unicode UTF-16 encoding, uses 2 bytes per character Windows applications, .NET strings
Encoding.UTF32 Fixed-width encoding, uses 4 bytes per character Applications requiring fixed-width Unicode

Conclusion

The Console.OutputEncoding property in C# provides control over how text is displayed in the console. Choose ASCII for basic English text, UTF-8 for international characters and web compatibility, or Unicode for Windows-specific applications requiring full Unicode support.

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

453 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements