Mono Class Library: System.Text.Encoding Overview | Members

System.Text.Encoding.GetByteCount Method

Returns the number of bytes required to encode the specified range of characters in the specified Unicode character array.

public abstract int GetByteCount (char[] chars, int index, int count)

Parameters

chars
The char array to encode.
index
A int containing the first index of chars to encode.
count
A int containing the number of characters to encode.

Returns

A int containing the number of bytes required to encode the range in chars from index to index + count - 1.

Exceptions

TypeReason
ArgumentNullExceptionchars is null .
ArgumentOutOfRangeException

The number of bytes required to encode the specified elements in chars is greater than int.MaxValue.

-or-

index or count is less than zero.

-or-

index and count do not specify a valid range in chars (i.e. (index + count) > chars.Length).

Remarks

Operation
As described above.

Note to Inheritors
This method is overridden by types derived from System.Text.Encoding to return the appropriate number of bytes for the particular encoding.

Usage

Encoding.GetByteCount(Char[]) can be used to determine the exact the number of bytes that will be produced from encoding a given range of characters. An appropriately sized buffer for that conversion can then be allocated.

Alternatively, Encoding.GetMaxByteCount(int) can be used to determine the maximum number of bytes that will be produced from converting a given number of characters, regardless of the actual character values. A buffer of that size can then be reused for multiple conversions.

Encoding.GetByteCount(Char[]) generally uses less memory and Encoding.GetMaxByteCount(int) generally executes faster.

Requirements

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0