Buffer::ByteLength Method (Array^)

 
System_CAPS_noteNote

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

Returns the number of bytes in the specified array.

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

public:
static int ByteLength(
	Array^ array
)

Parameters

array
Type: System::Array^

An array.

Return Value

Type: System::Int32

The number of bytes in the array.

Exception Condition
ArgumentNullException

array is null.

ArgumentException

array is not a primitive.

OverflowException

array is larger than 2 gigabytes (GB).

The following code example illustrates the use of the ByteLength method to return the number of bytes in an array.

// Example of the Buffer::ByteLength method.
using namespace System;

void ArrayInfo( Array^ arr, String^ name )
{
   int byteLength = Buffer::ByteLength( arr );

   // Display the array name, type, Length, and ByteLength.
   Console::WriteLine( "{0,10}{1,20}{2,9}{3,12}", name, arr->GetType(), arr->Length, byteLength );
}

int main()
{
   array<unsigned char>^bytes = {1,2,3,4,5,6,7,8,9,0};
   array<bool>^bools = {true,false,true,false,true};
   array<Char>^chars = {' ','$','\"','A','{'};
   array<short>^shorts = {258,259,260,261,262,263};
   array<float>^singles = {1,678,2.37E33F,.00415F,8.9F};
   array<double>^doubles = {2E-22,.003,4.4E44,555E55};
   array<long>^longs = {1,10,100,1000,10000,100000};
   Console::WriteLine( "This example of the Buffer::ByteLength( Array* ) "
   "\nmethod generates the following output.\n" );
   Console::WriteLine( "{0,10}{1,20}{2,9}{3,12}", "Array name", "Array type", "Length", "ByteLength" );
   Console::WriteLine( "{0,10}{1,20}{2,9}{3,12}", "----------", "----------", "------", "----------" );

   // Display the Length and ByteLength for each array.
   ArrayInfo( bytes, "bytes" );
   ArrayInfo( bools, "bools" );
   ArrayInfo( chars, "chars" );
   ArrayInfo( shorts, "shorts" );
   ArrayInfo( singles, "singles" );
   ArrayInfo( doubles, "doubles" );
   ArrayInfo( longs, "longs" );
}

/*
This example of the Buffer::ByteLength( Array* )
method generates the following output.

Array name          Array type   Length  ByteLength
----------          ----------   ------  ----------
     bytes       System.Byte[]       10          10
     bools    System.Boolean[]        5           5
     chars       System.Char[]        5          10
    shorts      System.Int16[]        6          12
   singles     System.Single[]        5          20
   doubles     System.Double[]        4          32
     longs      System.Int32[]        6          24
*/

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: