System.Text.ASCIIEncoding.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

The ASCIIEncoding.GetByteCount(Char[], int, int) method calculates the exact array size required by the ASCIIEncoding.GetBytes(string, int, int, Byte[], int) method to store the resulting bytes, whereas the ASCIIEncoding.GetMaxByteCount(int) method calculates the maximum array size. The ASCIIEncoding.GetByteCount(Char[], int, int) method generally allocates less memory, but the ASCIIEncoding.GetMaxByteCount(int) method generally executes faster.

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

The ASCIIEncoding.GetMaxByteCount(int) method considers potential leftover surrogates from a previous encoding operation. As a result, if the System.Text.ASCIIEncoding object uses the default replacement fallback, or if a custom replacement fallback has been defined with a single possible fallback character, the method returns charCount + 1. If the System.Text.ASCIIEncoding object uses a replacement fallback with more than one possible fallback character, the method returns n * (charCount + 1), where n is the maximum number of fallback characters.

ASCIIEncoding.GetMaxByteCount(int) has no relation to ASCIIEncoding.GetChars(Byte[], int, int, Char[], int). If your application needs a similar function to use with ASCIIEncoding.GetChars(Byte[], int, int, Char[], int), it should use ASCIIEncoding.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