Mono Class Library: System Namespace

System.Buffer Class

A low level array manipulation class. Allows bytewise reading and writing on the primitive types contained in an array. [Edit]

See Also: Buffer Members

[System.Runtime.InteropServices.ComVisible(true)]
public static class Buffer

Remarks

Using this class it is possible to access arrays of primitive types on byte level. For example an integer array declared as
C# Example
int[] arr = new int[10];
is lined up in memory as 10 consecutive 32 bit integers. This class can access the individual bytes, and hence circumvents any byte order abstractions. The following example shows how to use most methods of this class.
C# Example
using System;
using System.Text;

public class BufferTest {
        public static void Main (string[] args)
        {
                int[] arr1 = { 65, 66, 67, 68, 69, };
                byte[] arr2 = new byte[16];

                // prints "arr1 length in bytes is 20" (5 * 4)
                Console.WriteLine ("arr1 length in bytes is {0}",
                        Buffer.ByteLength (arr1));
                Buffer.BlockCopy (arr1, 1, arr2, 0, 12);

                // prints "BCD" (which is 66, 67, 68).
                // The NUL inbetween bytes are skipped.
                Console.WriteLine (Encoding.ASCII.GetString (arr2));

                // note that the following is endian dependant
                StringBuilder byteStr = new StringBuilder ();
                for (int n = 0 ; n < Buffer.ByteLength (arr1) ; ++n) {
                        byte b = Buffer.GetByte (arr1, n);

                        byteStr.Append ("0123456789abcdef"[b >> 4]);
                        byteStr.Append ("0123456789abcdef"[b & 0xf]);
                        byteStr.Append (" ");
                }

                Console.WriteLine ("hexdump of arr1: {0}", byteStr);
        }
}
[Edit]

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0