O'Reilly logo

.NET Common Language Runtime Unleashed by Kevin Burton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required