Buffer.ByteLength(Array) Method

Definition

Returns the number of bytes in the specified array.

C#
public static int ByteLength (Array array);

Parameters

array
Array

An array.

Returns

The number of bytes in the array.

Exceptions

array is null.

array is not a primitive.

array is larger than 2 gigabytes (GB).

Examples

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

C#
// Example of the Buffer.ByteLength method.
using System;

class ByteLengthDemo
{
    const string formatter = "{0,10}{1,20}{2,9}{3,12}";

    public static void ArrayInfo( Array arr, string name )
    {
        int byteLength = Buffer.ByteLength( arr );

        // Display the array name, type, Length, and ByteLength.
        Console.WriteLine( formatter, name, arr.GetType( ),
            arr.Length, byteLength );
    }

    public static void Main( )
    {
        byte[ ]   bytes   = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        bool[ ]   bools   = { true, false, true, false, true };
        char[ ]   chars   = { ' ', '$', '\"', 'A', '{' };
        short[ ]  shorts  = { 258, 259, 260, 261, 262, 263 };
        float[ ]  singles = { 1, 678, 2.37E33F, .00415F, 8.9F };
        double[ ] doubles = { 2E-22, .003, 4.4E44, 555E55 };
        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( formatter, "Array name", "Array type",
            "Length", "ByteLength" );
        Console.WriteLine( formatter, "----------", "----------",
            "------", "----------" );

        // 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.Int64[]        6          48
*/

Applies to

제품 버전
.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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0