Share via


Buffer.GetByte Method

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

Retrieves the byte at a specified location in a specified array.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function GetByte ( _
    array As Array, _
    index As Integer _
) As Byte
[SecuritySafeCriticalAttribute]
public static byte GetByte(
    Array array,
    int index
)

Parameters

Return Value

Type: System.Byte
Returns the index byte in the array.

Exceptions

Exception Condition
ArgumentException

array is not a primitive.

ArgumentNullException

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

ArgumentOutOfRangeException

index is negative or greater than the length of array.

Remarks

The GetByte method gets a particular byte out of the array. The array must be an array of primitives.

Examples

The following code example displays the byte values at specified locations within arrays using the GetByte method.

' Example of the Buffer.GetByte method.

Module Example

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

   ' Display the array contents in hexadecimal.
   Sub DisplayArray(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal arr As Array, ByVal name As String)

      ' Get the array element width; format the formatting string.
      Dim loopX As Integer
      Dim elemWidth As Integer = _
          Buffer.ByteLength(arr) / arr.Length
      Dim format As String = _
          String.Format(" {{0:X{0}}}", 2 * elemWidth)

      ' Display the array elements from right to left.
      outputBlock.Text &= String.Format("{0,7}:", name)
      For loopX = arr.Length - 1 To 0 Step -1
         outputBlock.Text &= String.Format(format, arr(loopX))
      Next loopX
      outputBlock.Text &= vbCrLf
   End Sub

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

      Dim value As Byte = Buffer.GetByte(arr, index)

      ' Display the array name, index, and byte to be viewed.
      outputBlock.Text &= formatter, name, index, value, _
          String.Format("&H{0:X2}", value) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' These are the arrays to be viewed with GetByte.
      Dim longs() As Long = _
          {333333333333333333, 666666666666666666, 999999999999999999}
      Dim ints() As Integer = _
          {111111111, 222222222, 333333333, 444444444, 555555555}

      outputBlock.Text &= "This example of the " & _
          "Buffer.GetByte( Array, Integer ) " & vbCrLf & _
          "method generates the following output." & vbCrLf & _
          "Note: The arrays are displayed from right to left." & vbCrLf
      outputBlock.Text &= vbCrLf & "  Values of arrays:" & vbCrLf & vbCrLf

      ' Display the values of the arrays.
      DisplayArray(outputBlock, longs, "longs")
      DisplayArray(outputBlock, ints, "ints")
      outputBlock.Text &= vbCrLf

      outputBlock.Text &= String.Format(formatter, "Array", "index", _
          "value", "") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "-----", "-----", _
          "-----", "----") & vbCrLf

      ' Display the Length and ByteLength for each array.
      ArrayInfo(outputBlock, ints, "ints", 0)
      ArrayInfo(outputBlock, ints, "ints", 7)
      ArrayInfo(outputBlock, ints, "ints", 10)
      ArrayInfo(outputBlock, ints, "ints", 17)
      ArrayInfo(outputBlock, longs, "longs", 0)
      ArrayInfo(outputBlock, longs, "longs", 6)
      ArrayInfo(outputBlock, longs, "longs", 10)
      ArrayInfo(outputBlock, longs, "longs", 17)
      ArrayInfo(outputBlock, longs, "longs", 21)
   End Sub
End Module

' This example of the Buffer.GetByte( Array, Integer )
' method generates the following output.
' Note: The arrays are displayed from right to left.
' 
'   Values of arrays:
' 
'   longs: 0DE0B6B3A763FFFF 094079CD1A42AAAA 04A03CE68D215555
'    ints: 211D1AE3 1A7DAF1C 13DE4355 0D3ED78E 069F6BC7
' 
'      Array     index    value
'      -----     -----    ----- ----
'       ints         0      199 &HC7
'       ints         7       13 &H0D
'       ints        10      222 &HDE
'       ints        17       26 &H1A
'      longs         0       85 &H55
'      longs         6      160 &HA0
'      longs        10       66 &H42
'      longs        17      255 &HFF
'      longs        21      182 &HB6
// Example of the Buffer.GetByte method.
using System;

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

   // Display the array contents in hexadecimal.
   public static void DisplayArray(System.Windows.Controls.TextBlock outputBlock, Array arr, string name)
   {
      // Get the array element width; format the formatting string.
      int elemWidth = Buffer.ByteLength(arr) / arr.Length;
      string format = String.Format(" {{0:X{0}}}", 2 * elemWidth);

      // Display the array elements from right to left.
      outputBlock.Text += String.Format("{0,5}:", name);
      for (int loopX = arr.Length - 1; loopX >= 0; loopX--)
         outputBlock.Text += String.Format(format, arr.GetValue(loopX));
      outputBlock.Text += "\n";
   }

   public static void ArrayInfo(System.Windows.Controls.TextBlock outputBlock, Array arr, string name, int index)
   {
      byte value = Buffer.GetByte(arr, index);

      // Display the array name, index, and byte to be viewed.
      outputBlock.Text += formatter, name, index, value,
          String.Format("0x{0:X2}", value) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // These are the arrays to be viewed with GetByte.
      long[] longs = { 333333333333333333, 666666666666666666, 999999999999999999 };
      int[] ints = { 111111111, 222222222, 333333333, 444444444, 555555555 };

      outputBlock.Text += "This example of the " +
          "Buffer.GetByte( Array, int ) \n" +
          "method generates the following output.\n" +
          "Note: The arrays are displayed from right to left.\n" + "\n";
      outputBlock.Text += "  Values of arrays:\n" + "\n";

      // Display the values of the arrays.
      DisplayArray(outputBlock, longs, "longs");
      DisplayArray(outputBlock, ints, "ints");
      outputBlock.Text += "\n";

      outputBlock.Text += String.Format(formatter, "Array", "index", "value", "") + "\n";
      outputBlock.Text += String.Format(formatter, "-----", "-----", "-----",
          "----") + "\n";

      // Display the Length and ByteLength for each array.
      ArrayInfo(outputBlock, ints, "ints", 0);
      ArrayInfo(outputBlock, ints, "ints", 7);
      ArrayInfo(outputBlock, ints, "ints", 10);
      ArrayInfo(outputBlock, ints, "ints", 17);
      ArrayInfo(outputBlock, longs, "longs", 0);
      ArrayInfo(outputBlock, longs, "longs", 6);
      ArrayInfo(outputBlock, longs, "longs", 10);
      ArrayInfo(outputBlock, longs, "longs", 17);
      ArrayInfo(outputBlock, longs, "longs", 21);
   }
}

/*
This example of the Buffer.GetByte( Array, int )
method generates the following output.
Note: The arrays are displayed from right to left.

  Values of arrays:

longs: 0DE0B6B3A763FFFF 094079CD1A42AAAA 04A03CE68D215555
 ints: 211D1AE3 1A7DAF1C 13DE4355 0D3ED78E 069F6BC7

     Array     index    value
     -----     -----    ----- ----
      ints         0      199 0xC7
      ints         7       13 0x0D
      ints        10      222 0xDE
      ints        17       26 0x1A
     longs         0       85 0x55
     longs         6      160 0xA0
     longs        10       66 0x42
     longs        17      255 0xFF
     longs        21      182 0xB6
*/

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