This documentation is archived and is not being maintained.

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

.NET Framework 1.1

Calculates the number of bytes required to store the results of encoding a set of characters from a specified Unicode character array.

[Visual Basic]
Overrides Overloads Public Function GetByteCount( _
   ByVal chars() As Char, _
   ByVal index As Integer, _
   ByVal count As Integer _
) As Integer
public override int GetByteCount(
 char[] chars,
 int index,
 int count
public: int GetByteCount(
 __wchar_t chars __gc[],
 int index,
 int count
public override function GetByteCount(
   chars : Char[],
 index : int,
 count : int
) : int;


The Unicode character array to encode.
The index of the first character in chars to encode.
The number of characters to encode.

Return Value

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


Exception Type Condition
ArgumentException Return value is greater than MaxValue.
ArgumentNullException chars is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException index is less than zero.


count is less than zero.


index plus count is greater than the length of chars.


Use GetByteCount to calculate exactly, or GetMaxByteCount to calculate at most, the array size required by the GetBytes method to store encoded characters.


[Visual Basic, C#, C++] The following example demonstrates how to use the GetByteCount method to return the number of bytes required to encode the array of Unicode characters chars using UnicodeEncoding.

[Visual Basic] 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UnicodeEncodingExample
    Public Shared Sub Main()
        ' Unicode characters.
        ' ChrW(35)  = #
        ' ChrW(37)  = %
        ' ChrW(928) = Pi
        ' ChrW(931) = Sigma
        Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
        Dim uni As New UnicodeEncoding()
        Dim byteCount As Integer = uni.GetByteCount(chars, 1, 2)
        Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
    End Sub 'Main
End Class 'UnicodeEncodingExample

using System;
using System.Text;

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

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

#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main()
   // Unicode characters.
   Char chars[] = 
      L'\u03a0', // Pi
      L'\u03a3', // Sigma

   UnicodeEncoding* Unicode = new UnicodeEncoding();
   int byteCount = Unicode -> GetByteCount(chars, 1, 2);
   Console::WriteLine(S"{0} bytes needed to encode characters.", __box(byteCount));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

UnicodeEncoding Class | UnicodeEncoding Members | System.Text Namespace | UnicodeEncoding.GetByteCount Overload List