When overridden in a derived class, calculates the maximum number of bytes produced by encoding the specified number of characters.
Assembly: 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, the application should use GetByteCount. To calculate the maximum array size, it should use . 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 , 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 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. Your application 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.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.