O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

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

Name

Buffer

Synopsis

The Buffer class provides static methods used to manipulate a region of unmanaged memory as though it were an array of Bytes. Byte arrays are traditionally used in unmanaged code to represent blocks of contiguous memory. This class includes the ByteLength() method, which indicates the total number of bytes in an array, and the GetByte() and SetByte() methods, which allow you to retrieve or set a specific Byte object in an array by specifying a zero-based index. Additionally, the BlockCopy() method can be used to move contiguous groups of bytes from one position in a buffer to another.

Note that Buffer ignores types when conducting its byte-shuffling operations. If you use Buffer to insert an otherwise incompatible type into the buffer, the copy goes through, but unpredictable results will arise later when you try to work with the buffer as its original type. For example, if you use the Buffer method to insert an Int32 into an array of String objects, the copy goes through, but the next time the array is accessed, there is no longer a String reference. It is an Int32, and what the CLR will do at that point is undocumented.

public sealed class Buffer {
// Public Static Methods
   public static method void BlockCopy(Array src, 
        int srcOffset, Array dst, int dstOffset, 
        int count);  
   public static method int ByteLength(Array array);  
   public static method byte GetByte(Array array, int index);  
   public static method void SetByte(Array array, int index, 
        byte value);  
}

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