UTF7Encoding.GetByteCount Method (Char[], Int32, Int32)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Calculates the number of bytes produced by encoding a set of characters from the specified character array.

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

public override int GetByteCount(
	char[] chars,
	int index,
	int count


Type: System.Char[]

The character array containing the set of characters to encode.

Type: System.Int32

The index of the first character to encode.

Type: System.Int32

The number of characters to encode.

Return Value

Type: System.Int32

The number of bytes produced by encoding the specified characters.

Exception Condition

chars is null (Nothing).


index or count is less than zero.


index and count do not denote a valid range in chars.


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


A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)


EncoderFallback is set to EncoderExceptionFallback.

To calculate the exact array size required by GetBytes to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, the application should use GetMaxByteCount. The GetByteCount method generally allows allocation of less memory, while the GetMaxByteCount method generally executes faster.

The following code example demonstrates how to use the GetByteCount method to return the number of bytes required to encode an array of Unicode characters.

using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma

        UTF7Encoding utf7 = new UTF7Encoding();
        int byteCount = utf7.GetByteCount(chars, 1, 2);
            "{0} bytes needed to encode characters.", byteCount

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top