Condividi tramite


Buffer.ByteLength Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the number of bytes in the specified array.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function ByteLength ( _
    array As Array _
) As Integer
[SecuritySafeCriticalAttribute]
public static int ByteLength(
    Array array
)

Parameters

Return Value

Type: System.Int32
The number of bytes in the array.

Exceptions

Exception Condition
ArgumentNullException

array is nulla null reference (Nothing in Visual Basic).

ArgumentException

array is not a primitive.

Examples

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.

Module Example

   Const formatter As String = "{0,10}{1,20}{2,9}{3,12}"

   Sub ArrayInfo(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal arr As Array, ByVal name As String)

      Dim byteLength As Integer = Buffer.ByteLength(arr)

      ' Display the array name, type, Length, and ByteLength.
      outputBlock.Text &= String.Format(formatter, name, arr.GetType, arr.Length, _
          byteLength) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim bytes() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
      Dim bools() As Boolean = {True, False, True, False, True}
      Dim chars() As Char = {" "c, "$"c, """"c, "A"c, "{"c}
      Dim shorts() As Short = {258, 259, 260, 261, 262, 263}
      Dim singles() As Single = {1, 678, 2.37E+33, 0.00415, 8.9}
      Dim doubles() As Double = {2.0E-22, 0.003, 4.4E+44, 5.55E+57}
      Dim longs() As Long = {1, 10, 100, 1000, 10000, 100000}

      outputBlock.Text &= String.Format( _
          "This example of the Buffer.ByteLength( Array ) " & _
          vbCrLf & "method generates the following output." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(formatter, "Array name", "Array type", _
          "Length", "ByteLength") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "----------", "----------", _
          "------", "----------") & vbCrLf

      ' Display the Length and ByteLength for each array.
      ArrayInfo(outputBlock, bytes, "bytes")
      ArrayInfo(outputBlock, bools, "bools")
      ArrayInfo(outputBlock, chars, "chars")
      ArrayInfo(outputBlock, shorts, "shorts")
      ArrayInfo(outputBlock, singles, "singles")
      ArrayInfo(outputBlock, doubles, "doubles")
      ArrayInfo(outputBlock, longs, "longs")
   End Sub
End Module

' 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
// Example of the Buffer.ByteLength method.
using System;

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

   public static void ArrayInfo(System.Windows.Controls.TextBlock outputBlock, Array arr, string name)
   {
      int byteLength = Buffer.ByteLength(arr);

      // Display the array name, type, Length, and ByteLength.
      outputBlock.Text += String.Format(formatter, name, arr.GetType(),
          arr.Length, byteLength) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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 };

      outputBlock.Text += String.Format(
          "This example of the Buffer.ByteLength( Array ) " +
          "\nmethod generates the following output.\n") + "\n";
      outputBlock.Text += String.Format(formatter, "Array name", "Array type",
          "Length", "ByteLength") + "\n";
      outputBlock.Text += String.Format(formatter, "----------", "----------",
          "------", "----------") + "\n";

      // Display the Length and ByteLength for each array.
      ArrayInfo(outputBlock, bytes, "bytes");
      ArrayInfo(outputBlock, bools, "bools");
      ArrayInfo(outputBlock, chars, "chars");
      ArrayInfo(outputBlock, shorts, "shorts");
      ArrayInfo(outputBlock, singles, "singles");
      ArrayInfo(outputBlock, doubles, "doubles");
      ArrayInfo(outputBlock, 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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference