This documentation is archived and is not being maintained.

Encoding.GetEncoding Method (String, EncoderFallback, DecoderFallback)

Returns the encoding associated with the specified code page name. Parameters specify an error handler for characters that cannot be encoded and byte sequences that cannot be decoded.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

public static Encoding GetEncoding(
	string name,
	EncoderFallback encoderFallback,
	DecoderFallback decoderFallback


Type: System.String
The code page name of the preferred encoding. Any value returned by the WebName property is valid. Possible values are listed in the Name column of the table that appears in the Encoding class topic.
Type: System.Text.EncoderFallback
An object that provides an error-handling procedure when a character cannot be encoded with the current encoding.
Type: System.Text.DecoderFallback
An object that provides an error-handling procedure when a byte sequence cannot be decoded with the current encoding.

Return Value

Type: System.Text.Encoding
The encoding that is associated with the specified code page.


name is not a valid code page name.


The code page indicated by name is not supported by the underlying platform.

The GetEncoding method relies on the underlying platform to support most code pages. However, the .NET Framework natively supports some encodings.


The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, applications should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page.

For a list of code pages, see the Encoding class topic. The application uses the GetEncodings method to get a list of all encodings.

GetEncoding returns a cached instance with default settings. The application should use the constructors of derived classes to get an instance with different settings. For example, the UTF32Encoding class provides a constructor that lets you enable error detection.

The following example demonstrates the Encoding.GetEncoding(String, EncoderFallback, DecoderFallback) method.

// This example demonstrates the EncoderReplacementFallback class.

using System;
using System.Text;

class Sample 
    public static void Main() 

// Create an encoding, which is equivalent to calling the 
// ASCIIEncoding class constructor. 
// The EncoderReplacementFallback parameter specifies that the
// string, "(unknown)", replace characters that cannot be encoded. 
// A decoder replacement fallback is also specified, but in this 
// code example the decoding operation cannot fail.  

    Encoding ae = Encoding.GetEncoding(
                  new EncoderReplacementFallback("(unknown)"), 
                  new DecoderReplacementFallback("(error)"));

// The input string consists of the Unicode characters LEFT POINTING 
// The encoding can only encode characters in the US-ASCII range of U+0000 
// through U+007F. Consequently, the characters bracketing the 'X' character
// are replaced with the fallback replacement string, "(unknown)".

    string inputString = "\u00abX\u00bb";
    string decodedString;
    string twoNewLines = "\n\n";
    byte[] encodedBytes = new byte[ae.GetByteCount(inputString)];
    int numberOfEncodedBytes = 0;
    int ix = 0;

// --------------------------------------------------------------------------

// Display the name of the encoding.
    Console.WriteLine("The name of the encoding is \"{0}\".\n", ae.WebName);

// Display the input string in text.
    Console.WriteLine("Input string ({0} characters): \"{1}\"", 
                       inputString.Length, inputString);

// Display the input string in hexadecimal.
    Console.Write("Input string in hexadecimal: ");
    foreach (char c in inputString.ToCharArray()) 
        Console.Write("0x{0:X2} ", (int)c);

// --------------------------------------------------------------------------
// Encode the input string. 

    Console.WriteLine("Encode the input string...");
    numberOfEncodedBytes = ae.GetBytes(inputString, 0, inputString.Length, 
                                       encodedBytes, 0);

// Display the encoded bytes.
    Console.WriteLine("Encoded bytes in hexadecimal ({0} bytes):\n", 
    ix = 0;
    foreach (byte b in encodedBytes)
        Console.Write("0x{0:X2} ", (int)b);
        if (0 == ix % 6) Console.WriteLine();

// --------------------------------------------------------------------------
// Decode the encoded bytes, yielding a reconstituted string.

    Console.WriteLine("Decode the encoded bytes...");
    decodedString = ae.GetString(encodedBytes);

// Display the input string and the decoded string for comparison.
    Console.WriteLine("Input string:  \"{0}\"", inputString);
    Console.WriteLine("Decoded string:\"{0}\"", decodedString);
This code example produces the following results:

The name of the encoding is "us-ascii".

Input string (3 characters): "�X�"
Input string in hexadecimal: 0xAB 0x58 0xBB

Encode the input string...
Encoded bytes in hexadecimal (19 bytes):

0x28 0x75 0x6E 0x6B 0x6E 0x6F
0x77 0x6E 0x29 0x58 0x28 0x75
0x6E 0x6B 0x6E 0x6F 0x77 0x6E

Decode the encoded bytes...
Input string:  "�X�"
Decoded string:"(unknown)X(unknown)"


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.