Export (0) Print
Expand All

UTF8Encoding.GetMaxByteCount Method

Calculates the maximum number of bytes produced by encoding the specified number of characters.

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

public override int GetMaxByteCount (
	int charCount
)
public int GetMaxByteCount (
	int charCount
)
public override function GetMaxByteCount (
	charCount : int
) : int

Parameters

charCount

The number of characters to encode.

Return Value

The maximum number of bytes produced by encoding the specified number of characters.

Exception typeCondition

ArgumentOutOfRangeException

charCount is less than zero.

-or-

The resulting number of bytes is greater than the maximum number that can be returned as an int.

To calculate the exact array size required by GetBytes to store the resulting bytes, use GetByteCount. To calculate the maximum array size, use GetMaxByteCount. The GetByteCount method generally allows you to allocate less memory, whereas the GetMaxByteCount method generally executes faster.

The following example demonstrates how to use the GetMaxByteCount method to return the maximum number of bytes required to encode a specified number of characters.

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        int charCount = 2;
        int maxByteCount = utf8.GetMaxByteCount(charCount);
        Console.WriteLine(
            "Maximum of {0} bytes needed to encode {1} characters.",
            maxByteCount,
            charCount
        );
    }
}

import System.*;
import System.Text.*;

class UTF8EncodingExample
{
    public static void main(String[] args)
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        int charCount = 2;
        int maxByteCount = utf8.GetMaxByteCount(charCount);

        Console.WriteLine(
            "Maximum of {0} bytes needed to encode {1} characters.", 
            String.valueOf(maxByteCount), String.valueOf(charCount));
    } //main
} //UTF8EncodingExample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft