System.Text.UnicodeEncoding.GetEncoder Method

Obtains an encoder that converts a sequence of Unicode characters into a UTF-16 encoded sequence of bytes.

Syntax

[System.Runtime.InteropServices.ComVisible(false)]
public override Encoder GetEncoder ()

Returns

A System.Text.Encoder object that converts a sequence of Unicode characters into a UTF-16 encoded sequence of bytes.

Remarks

The Encoder.GetBytes(Char[], int, int, Byte[], int, bool) method converts sequential blocks of characters into sequential blocks of bytes in a manner similar to the UnicodeEncoding.GetBytes(string, int, int, Byte[], int) method of this class. However, a System.Text.Encoder object maintains state information between calls so that it can correctly encode character sequences that span blocks. The System.Text.Encoder object also preserves trailing characters at the end of data blocks and uses the trailing characters in the next encoding operation. For example, a data block might end with an unmatched high surrogate, and the matching low surrogate might be in the next data block. Therefore, UnicodeEncoding.GetDecoder and UnicodeEncoding.GetEncoder are useful for network transmission and file operations, because those operations often deal with blocks of data instead of a complete data stream.

If error detection is enabled, that is, the throwOnInvalidBytes parameter of the constructor is set to true, error detection is also enabled in the System.Text.Encoder object returned by this method. If error detection is enabled and an invalid sequence is encountered, the state of the encoder is undefined and processing must stop.

Requirements

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