System.Text.UTF8Encoding.GetMaxByteCount Method

Calculates the maximum number of bytes produced by encoding the specified number of characters.

Syntax

public override int GetMaxByteCount (int charCount)

Parameters

charCount
The number of characters to encode.

Returns

The maximum number of bytes produced by encoding the specified number of characters.

Exceptions

TypeReason
ArgumentOutOfRangeException charCount < 0.

Remarks

To calculate the exact array size required by UTF8Encoding.GetBytes(string, int, int, Byte[], int) to store the resulting bytes, the application uses UTF8Encoding.GetByteCount(Char[], int, int). To calculate the maximum array size, the application should use UTF8Encoding.GetMaxByteCount(int). The UTF8Encoding.GetByteCount(Char[], int, int) method generally allows allocation of less memory, while the UTF8Encoding.GetMaxByteCount(int) method generally executes faster.

UTF8Encoding.GetMaxByteCount(int) is a worst-case number, including the worst case for the currently selected System.Text.EncoderFallback. If a fallback is chosen with a potentially large string, UTF8Encoding.GetMaxByteCount(int) can return large values.

In most cases, this method returns reasonable numbers for small strings. For large strings, you might have to choose between using very large buffers and catching errors in the rare case that a more reasonable buffer is exceeded. You might also want to consider a different approach using erload:System.Text.UTF8Encoding.GetByteCount or erload:System.Text.Encoder.Convert. For example, text in English and many other languages often needs only one UTF-8 byte to represent a character, but the number returned by UTF8Encoding.GetMaxByteCount(int) has to allow for the possibility that the string to be converted will consist entirely of characters that each require four bytes.

UTF8Encoding.GetMaxByteCount(int) has no relation to UTF8Encoding.GetChars(Byte[], int, int, Char[], int). If your application needs a similar function to use with UTF8Encoding.GetChars(Byte[], int, int, Char[], int), it should use UTF8Encoding.GetMaxCharCount(int).

Note:

GetMaxByteCount(N) is not necessarily the same value as N* GetMaxByteCount(1).

Requirements

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