Encoding.GetEncoding Method (Int32, EncoderFallback, DecoderFallback)


Returns the encoding associated with the specified code page identifier. 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)

static member GetEncoding : 
        codepage:int *
        encoderFallback:EncoderFallback *
        decoderFallback:DecoderFallback -> Encoding


Type: System.Int32

The code page identifier of the preferred encoding. Possible values are listed in the Code Page column of the table that appears in the Encoding class topic.


0 (zero), to use the default encoding.

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.

Exception Condition

codepage is less than zero or greater than 65535.


codepage is not supported by the underlying platform.


codepage is not supported by the underlying platform.


Some unsupported code pages cause the exception ArgumentException to be thrown, whereas others cause NotSupportedException. Therefore, your code must catch all exceptions indicated in the Exceptions section.

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

In addition to the encodings that are intrinsically supported on a specific platform version of the .NET Framework, the GetEncoding(Int32, EncoderFallback, DecoderFallback) method returns any additional encodings that are made available by registering an EncodingProvider object.


The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For this reason, encoding and decoding data using the default code page returned by Encoding.GetEncoding(0) is not recommended. For the most consistent results, you 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. You can call the GetEncodings method in the full .NET Framework on the Windows desktop to get a list of all encodings.

To get the encoding associated with the default ANSI code page in the operating system's regional and language settings, you can either supply a value of 0 for the codepage argument or, if your code is running on the full .NET Framework on the Windows desktop, retrieve the value of the Encoding.Default property. To determine the default code pages used on the system, use the Windows GetSystemDefaultLangID function. To determine the current ANSI code page, call the Windows GetACP function from the full .NET Framework on the Windows desktop.

GetEncoding returns a cached instance with default settings. You 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.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Return to top