GetEncoding Method

Encoding.GetEncoding Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns the encoding associated with the specified name.

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

public static Encoding GetEncoding(
	string name


Type: System.String
The name of the preferred encoding.

Return Value

Type: System.Text.Encoding
The object associated with the specified name.


name is not the name of a valid encoding.

The following table lists valid values for the name parameter. Except for "utf-16LE", they correspond to the strings returned by each encoding's WebName property.






UnicodeEncoding (little-endian)


UnicodeEncoding (big-endian)


UnicodeEncoding (little-endian)

The name parameter is not case-sensitive. For example, the method returns a UnicodeEncoding object if name is either "utf-16" or 'UTF-16".

GetEncoding returns a cached instance with default settings. To get an instance with different settings, call the appropriate constructors of derived classes. For example, the UnicodeEncoding class provides a constructor that allows enabling of error detection.

Version Notes

Windows Phone

 If you pass an invalid code page name to GetEncoding, the method throws PlatformNotSupportedException instead of ArgumentException.

The following example instantiates several encoding objects, including two that are returned by the GetEncoding method. It then checks them for equality.

using System;
using System.Text;

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      // Get a UTF-16 encoding.
      Encoding e16_1 = Encoding.GetEncoding("utf-16");

      // Instantiate a second UTF-16 encoding.
      Encoding e16_2 = new UnicodeEncoding();

      // Instantiate a Unicode little endian encoding.
      Encoding le = new UnicodeEncoding(false, true);

      // Instantiate a Unicode big endian encoding.
      Encoding be = new UnicodeEncoding(true, true);

      // Get a UTF-8 encoding by name.
      Encoding e8 = Encoding.GetEncoding("utf-8");

      // Check equality of e16_1 and e16_2.
      outputBlock.Text += String.Format("{0} equals {1}? {2}\n", 
                                        e16_1.WebName, e16_2.WebName, 

      // Check equality of e16_1 and unicode little endian encoding.
      outputBlock.Text += String.Format("{0} equals {1}? {2}\n", 
                                        e16_1.WebName, le.WebName, 

      // Check equality of e16_1 and unicode bug endian encoding.
      outputBlock.Text += String.Format("{0} equals {1}? {2}\n", 
                                        e16_1.WebName, be.WebName, 

      // Check equality of e16_1 and e8.
      outputBlock.Text += String.Format("{0} equals {1}? {2}\n", 
                                        e16_1.WebName, e8.WebName, 


This example produces the following output:
   utf-16 equals utf-16? True
   utf-16 equals utf-16? True
   utf-16 equals utf16BE? False
   utf-16 equals utf8? False

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft