This documentation is archived and is not being maintained.

UnicodeEncoding.GetMaxCharCount Method

Calculates the maximum number of characters that can result from decoding a specified number of bytes.

[Visual Basic]
Overrides Public Function GetMaxCharCount( _
   ByVal byteCount As Integer _
) As Integer
[C#]
public override int GetMaxCharCount(
 int byteCount
);
[C++]
public: int GetMaxCharCount(
 int byteCount
);
[JScript]
public override function GetMaxCharCount(
   byteCount : int
) : int;

Parameters

byteCount
The number of bytes to decode.

Return Value

The maximum number of characters that can result from decoding byteCount number of bytes.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException byteCount < 0.

-or-

byteCount is too large to store as type Int32.

Remarks

Use GetCharCount to calculate exactly, or GetMaxCharCount to calculate at most, the array size required by the GetChars method to store decoded characters.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetMaxCharCount method to return the maximum number of characters that can result from decoding a specified number of bytes stored in the variable byteCount.

[Visual Basic] 
Imports System
Imports System.Text

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim uni As New UnicodeEncoding()
        Dim byteCount As Integer = 8
        Dim maxCharCount As Integer = uni.GetMaxCharCount(byteCount)
        Console.WriteLine("Maximum of {0} characters needed to decode {1} bytes.", maxCharCount, byteCount)
    End Sub 'Main
End Class 'UnicodeEncodingExample

[C#] 
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        UnicodeEncoding Unicode = new UnicodeEncoding();
        int byteCount = 8;
        int maxCharCount = Unicode.GetMaxCharCount(byteCount);
        Console.WriteLine(
            "Maximum of {0} characters needed to decode {1} bytes.",
            maxCharCount,
            byteCount
        );
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main()
{
   UnicodeEncoding* Unicode = new UnicodeEncoding();
   int byteCount = 8;
   int maxCharCount = Unicode -> GetMaxCharCount(byteCount);
   Console::WriteLine(S"Maximum of {0} characters needed to decode {1} bytes.",
      __box(maxCharCount), __box(byteCount));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

UnicodeEncoding Class | UnicodeEncoding Members | System.Text Namespace | GetChars | GetChars

Show: