System.Text.UnicodeEncoding.GetChars Method

Decodes a sequence of bytes from the specified byte array into the specified character array.

Syntax

public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)

Parameters

bytes
The byte array containing the sequence of bytes to decode.
byteIndex
The index of the first byte to decode.
byteCount
The number of bytes to decode.
chars
The character array to contain the resulting set of characters.
charIndex
The index at which to start writing the resulting set of characters.

Returns

The actual number of characters written into chars.

Exceptions

TypeReason
ArgumentException chars does not contain sufficient space to store the decoded characters.
ArgumentNullException

chars is null.

-or-

bytes is null.

ArgumentOutOfRangeException

byteIndex < 0.

-or-

byteCount < 0.

-or-

charIndex < 0.

-or-

byteIndex and byteCount do not specify a valid range in bytes (i.e. (byteIndex + byteCount ) > bytes.Length).

-or-

charIndex > chars.Length.

Remarks

To calculate the exact array size required by UnicodeEncoding.GetChars(Byte[], int, int, Char[], int) to store the resulting characters, the application uses UnicodeEncoding.GetCharCount(Byte[], int, int). To calculate the maximum array size, the application should use UnicodeEncoding.GetMaxCharCount(int). The UnicodeEncoding.GetCharCount(Byte[], int, int) method generally allows allocation of less memory, while the UnicodeEncoding.GetMaxCharCount(int) method generally executes faster.

With error detection, an invalid sequence causes this method to throw a ArgumentException. Without error detection, invalid sequences are ignored, and no exception is thrown.

Data to be converted, such as data read from a stream, might be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the System.Text.Decoder or the System.Text.Encoder provided by the UnicodeEncoding.GetDecoder method or the UnicodeEncoding.GetEncoder method, respectively.

Requirements

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