UTF7Encoding.GetByteCount Method

Definition

Calculates the number of bytes produced by encoding a set of characters.

Overloads

GetByteCount(String)

Calculates the number of bytes produced by encoding the characters in the specified String object.

GetByteCount(Char*, Int32)

Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.

GetByteCount(Char[], Int32, Int32)

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

GetByteCount(String)

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

Calculates the number of bytes produced by encoding the characters in the specified String object.

C#
public override int GetByteCount (string s);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (string s);

Parameters

s
String

The String object containing the set of characters to encode.

Returns

The number of bytes produced by encoding the specified characters.

Attributes

Exceptions

s is null (Nothing).

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

A fallback occurred (for more information, see Character Encoding in .NET).

-and-

EncoderFallback is set to EncoderExceptionFallback.

Examples

The following code example demonstrates how to use the GetByteCount method to return the number of bytes required to encode a character array.

C#
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);
        Console.WriteLine(
            "{0} bytes needed to encode characters.", byteCount
        );
    }
}

Remarks

To calculate the exact array size that GetBytes requires 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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetByteCount(Char*, Int32)

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

Important

This API is not CLS-compliant.

Calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer.

C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetByteCount (char* chars, int count);
C#
[System.CLSCompliant(false)]
public override int GetByteCount (char* chars, int count);
C#
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);
C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);

Parameters

chars
Char*

A pointer to the first character to encode.

count
Int32

The number of characters to encode.

Returns

The number of bytes produced by encoding the specified characters.

Attributes

Exceptions

chars is null (Nothing in Visual Basic .NET).

count is less than zero.

-or-

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

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

EncoderFallback is set to EncoderExceptionFallback.

Remarks

To calculate the exact array size that GetBytes requires 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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetByteCount(Char[], Int32, Int32)

Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs
Source:
UTF7Encoding.cs

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

C#
public override int GetByteCount (char[] chars, int index, int count);

Parameters

chars
Char[]

The character array containing the set of characters to encode.

index
Int32

The index of the first character to encode.

count
Int32

The number of characters to encode.

Returns

The number of bytes produced by encoding the specified characters.

Exceptions

chars is null (Nothing).

index or count is less than zero.

-or-

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

-or-

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

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

EncoderFallback is set to EncoderExceptionFallback.

Examples

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.

C#
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);
        Console.WriteLine(
            "{0} bytes needed to encode characters.", byteCount
        );
    }
}

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0