System.Buffer

The Buffer class allows the programmer to perform simple manipulation of a managed type as if it were unmanaged. Essentially, the type of the data is ignored.

Listing B.2 shows an example of manipulating an integer array without respect to its type (buffer.cs).

Listing B.2. System.Buffer Sample
public class BufferMain
{
    static void Main(string [] args)
    {
        int [] rawData = new int [] {1,2,3,4} ;
        Console.WriteLine("Array byte length: {0} ",
                           Buffer.ByteLength(rawData));
        for(int i = 0;i < Buffer.ByteLength(rawData); i++)
        {
            if((i % 4) == 0)
                Console.WriteLine();
            Console.Write("0x{0:X}  ", Buffer.GetByte(rawData, i));
        }
    }
}

This array contains 4 integers. Each integer requires 4 bytes of storage, so the array takes up 16 bytes. The output ...

Get .NET Common Language Runtime Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.