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

To change the Input Encoding Scheme of the Console in C#, use the Console.InputEncoding property. This property allows you to specify how the console interprets input characters, which is particularly useful when working with different character sets or international text.

Syntax

Following is the syntax for setting the console input encoding −

Console.InputEncoding = Encoding.EncodingType;

To retrieve the current input encoding −

Encoding currentEncoding = Console.InputEncoding;

Common Encoding Types

Encoding Type Description
Encoding.UTF8 UTF-8 encoding for Unicode characters
Encoding.ASCII ASCII encoding for basic English characters
Encoding.Unicode UTF-16 encoding for Unicode characters
Encoding.UTF32 UTF-32 encoding for Unicode characters

Example

Let us see an example demonstrating how to change and display the input encoding scheme −

using System;
using System.Text;

class Demo {
   public static void Main(string[] args) {
      Console.WriteLine("Default Input Encoding: " + Console.InputEncoding);
      
      Console.InputEncoding = Encoding.ASCII;
      Console.WriteLine("Input Encoding changed to: " + Console.InputEncoding);
      
      Console.InputEncoding = Encoding.UTF8;
      Console.WriteLine("Input Encoding changed to: " + Console.InputEncoding);
      
      Console.InputEncoding = Encoding.Unicode;
      Console.WriteLine("Input Encoding changed to: " + Console.InputEncoding);
   }
}

The output of the above code is −

Default Input Encoding: System.Text.UTF8Encoding
Input Encoding changed to: System.Text.ASCIIEncoding
Input Encoding changed to: System.Text.UTF8Encoding
Input Encoding changed to: System.Text.UnicodeEncoding

Using UTF-8 for International Characters

When working with international characters, UTF-8 encoding is recommended −

using System;
using System.Text;

class InternationalDemo {
   public static void Main(string[] args) {
      Console.OutputEncoding = Encoding.UTF8;
      Console.InputEncoding = Encoding.UTF8;
      
      Console.WriteLine("Current Input Encoding: " + Console.InputEncoding.EncodingName);
      Console.WriteLine("Supports international characters: ñ, ü, é, ??, ???????");
      Console.WriteLine("Code page: " + Console.InputEncoding.CodePage);
   }
}

The output of the above code is −

Current Input Encoding: Unicode (UTF-8)
Supports international characters: ñ, ü, é, ??, ???????
Code page: 65001

How It Works

The Console.InputEncoding property determines how bytes received from the console input stream are converted to characters. When you change this property, the console uses the specified encoding to interpret keyboard input. This is essential for applications that need to handle non-ASCII characters or work in multilingual environments.

Console Input Encoding Flow Keyboard Input InputEncoding Property Character Interpretation Bytes ? Encoding ? Characters

Conclusion

The Console.InputEncoding property in C# allows you to control how input characters are interpreted by the console. Setting it to appropriate encoding schemes like UTF-8 ensures proper handling of international characters and multilingual text input in console applications.

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

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements