Skip to Main Content
C# in a Nutshell
book

C# in a Nutshell

by Ben Albahari, Ted Neward, Peter Drayton
March 2002
Intermediate to advanced content levelIntermediate to advanced
864 pages
31h 8m
English
O'Reilly Media, Inc.
Content preview from C# in a Nutshell

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);  
}
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# 8.0 in a Nutshell

C# 8.0 in a Nutshell

Joseph Albahari, Eric Johannsen
C# 10 in a Nutshell

C# 10 in a Nutshell

Joseph Albahari
C# in a Nutshell, Second Edition

C# in a Nutshell, Second Edition

Peter Drayton, Ben Albahari, Ted Neward
Code like a Pro in C#

Code like a Pro in C#

Jort Rodenburg

Publisher Resources

ISBN: 0596001819Catalog PageErrata