When overridden in a derived class, calculates the maximum number of bytes produced by encoding the specified number of characters.
Assemblies: System.Text.Encoding (in System.Text.Encoding.dll)
mscorlib (in mscorlib.dll)
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 GetBytes to store the resulting bytes, you should use the GetByteCount method. To calculate the maximum array size, use the method. The GetByteCount method generally allows allocation of less memory, while the method generally executes faster.
retrieves a worst-case number, including the worst case for the currently selected EncoderFallback. If a fallback is chosen with a potentially large string, retrieves large values, particularly in cases where the worst case for the encoding involves switching modes for every character. For example, this can happen for ISO-2022-JP. For more information, see the blog entry "What's with Encoding.GetMaxByteCount() and Encoding.GetMaxCharCount()?" (http://blogs.msdn.com/shawnste/archive/2005/03/02/383903.aspx).
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 , you should allocate the output buffer based on the maximum size of the input buffer. If the output buffer is constrained in size, you might use the Convert method.
Note that considers potential leftover surrogates from a previous decoder operation. Because of the decoder, passing a value of 1 to the method retrieves 2 for a single-byte encoding, such as ASCII. You should use the IsSingleByte property if this information is necessary.
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.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8