Encoding::GetMaxByteCount Method
When overridden in a derived class, calculates the maximum number of bytes produced by encoding the specified number of characters.
Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Parameters
- charCount
- Type: System::Int32
The number of characters to encode.
Return Value
Type: System::Int32The maximum number of bytes produced by encoding the specified number of characters.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | charCount is less than zero. |
| EncoderFallbackException | A fallback occurred (see Understanding Encodings for complete explanation). |
The charCount parameter actually specifies the number of Char objects that represent the Unicode characters to encode, because the .NET Framework internally uses UTF-16 to represent Unicode characters. Consequently, most Unicode characters can be represented by one Char object, but a Unicode character represented by a surrogate pair, for example, requires two Char objects.
To calculate the exact array size required by the GetBytes method to store the resulting bytes, the application should use GetByteCount. To calculate the maximum array size, it should use GetMaxByteCount, which returns a worst-case number. The GetByteCount method generally allocates less memory, while the GetMaxByteCount method generally executes faster.
In most cases, this method retrieves reasonable values for small strings. For large strings, you might have to choose between using very large buffers and catching errors in the rare case when a more reasonable buffer is too small. You might also want to consider a different approach using GetByteCount or Encoder::Convert.
When using GetMaxByteCount, your application should allocate the output buffer based on the maximum size of the input buffer. If the output buffer is constrained in size, the application might use the Convert method.
Note: |
|---|
GetMaxByteCount(N) is not necessarily the same value as N* GetMaxByteCount(1). |
All Encoding implementations must guarantee that no buffer overflow exceptions occur if buffers are sized according to the results of this method's calculations.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Note: