System.Text.Encoding.GetEncoding Method

Returns the encoding associated with the specified code page identifier.


public static Encoding GetEncoding (int codepage)


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


The encoding that is associated with the specified code page.


The fallback handler depends on the encoding type of codepage. If codepage is a code page or double-byte character set (DBCS) encoding, a best-fit fallback handler is used. Otherwise, a replacement fallback handler is used. These fallback handlers may not be appropriate for your application. To specify the fallback handler used by the encoding specified by codepage, you can call the Encoding.GetEncoding(int, EncoderFallback, DecoderFallback) overload.

The Encoding.GetEncoding(int) method relies on the underlying platform to support most code pages. However, the .NET Framework natively supports some encodings. For a list of code pages, see the System.Text.Encoding class topic. Alternatively, you can call the Encoding.GetEncodings method to get an array of System.Text.EncodingInfo objects that contains information about all encodings.

To get the encoding associated with the default ANSI code page in the operating system's regional and language settings, the application can either use a setting of 0 for the codepage parameter or 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, use the Windows GetACP function.


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 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, applications should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page.

Encoding.GetEncoding(int) 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 System.Text.UTF32Encoding class provides a constructor that lets you enable error detection.


Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,