Array - GetByte

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

Try it

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

public static void ArrayInfo( Array arr, string name, int index )
{
	// C# Extension Method: Array - GetByte
    byte value = arr.GetByte(index);

    // Display the array name, index, and byte to be viewed.
    Console.WriteLine( formatter, name, index, value, String.Format( "0x{0:X2}", value ) );
}

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

    Console.WriteLine( "This example of the Array.GetByte(int) method generates the following output.\n" );
	
    Console.WriteLine( formatter, "Array", "index", "value", "" );
    Console.WriteLine( formatter, "-----", "-----", "-----", "----" );

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

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     Retrieves the byte at a specified location in a specified array.
    /// </summary>
    /// <param name="array">An array.</param>
    /// <param name="index">A location in the array.</param>
    /// <returns>Returns the  byte in the array.</returns>
    public static Byte GetByte(this Array array, Int32 index)
    {
        return Buffer.GetByte(array, index);
    }
}