System.Text.UnicodeEncoding Class

Represents a UTF-16 encoding of Unicode characters.

See Also: UnicodeEncoding Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class UnicodeEncoding : Encoding

Remarks

Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. Decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters.

The tp://go.microsoft.com/fwlink/?linkid=37123 assigns a code point (a number) to each character in every supported script. A Unicode Transformation Format (UTF) is a way to encode that code point. The tp://go.microsoft.com/fwlink/?linkid=37123 uses the following UTFs:

Note:

The UTF-7 encoding supports certain protocols for which it is required, most often e-mail or newsgroup protocols. Since UTF-7 is not particularly secure or robust, it should generally not be used. UTF-8 should normally be preferred to UTF-7.

For more information about the UTFs and other encodings supported by System.Text, see Understanding Encodings.

The UnicodeEncoding.GetByteCount(Char[], int, int) method determines how many bytes result in encoding a set of Unicode characters, and the UnicodeEncoding.GetBytes(string, int, int, Byte[], int) method performs the actual encoding.

Likewise, the UnicodeEncoding.GetCharCount(Byte[], int, int) method determines how many characters result in decoding a sequence of bytes, and the UnicodeEncoding.GetChars(Byte[], int, int, Char[], int) and UnicodeEncoding.GetString(Byte[], int, int) methods perform the actual decoding.

System.Text.UnicodeEncoding corresponds to the Windows code pages 1200 (little endian byte order) and 1201 (big endian byte order).

The encoder can use the big endian byte order (most significant byte first) or the little endian byte order (least significant byte first). For example, the Latin Capital Letter A (code point U+0041) is serialized as follows (in hexadecimal):

It is generally more efficient to store Unicode characters using the native byte order. For example, it is better to use the little endian byte order on little endian platforms, such as Intel computers.

Optionally, the System.Text.UnicodeEncoding object provides a preamble, which is an array of bytes that can be prefixed to the sequence of bytes resulting from the encoding process. If the preamble contains a byte order mark (BOM), it helps the decoder determine the byte order and the transformation format or UTF. The UnicodeEncoding.GetPreamble method retrieves an array of bytes that can include the BOM.

Note:

To enable error detection and to make the class instance more secure, the application should use the UnicodeEncoding.#ctor constructor that takes a throwOnInvalidBytes parameter, and set that parameter to true. With error detection, a method that detects an invalid sequence of characters or bytes throws a ArgumentException. Without error detection, no exception is thrown, and the invalid sequence is generally ignored.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Requirements

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