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

System.Text.Encoding.GetByteCount Method

Returns the number of bytes required to encode the specified string.

public virtual int GetByteCount (string s)

Parameters

s
The string to decode.

Returns

A int containing the number of bytes needed to encode s.

Exceptions

TypeReason
ArgumentNullExceptions is null.

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 number of bytes that will be produced from encoding the given string . 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