Export (0) Print
Expand All

Buffer Class

Manipulates arrays of primitive types.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public static class Buffer
/** @attribute ComVisibleAttribute(true) */ 
public final class Buffer
ComVisibleAttribute(true) 
public final class Buffer
Not applicable.

Buffer only affects arrays of primitive types; this class does not apply to objects. Each primitive type is treated as a series of bytes without regard to any behavior or limitation associated with the primitive type.

Buffer provides methods to copy bytes from one array of primitive types to another array of primitive types, get a byte from an array, set a byte in an array, and obtain the length of an array. This class provides better performance for manipulating primitive types than similar methods in the System.Array class.

Buffer is applicable to the following primitive types: Boolean, Char, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Single, and Double.

The following code example illustrates the use of several Buffer class methods.

// Example of the Buffer class methods.
using System;

class BufferClassDemo
{
    // Display the array elements from right to left in hexadecimal.
    public static void DisplayArray( short[ ] arr )
    {
        Console.Write( "  arr:" );
        for( int loopX = arr.Length - 1; loopX >= 0; loopX-- )
            Console.Write( " {0:X4}", arr[ loopX ] );
        Console.WriteLine( );
    }

    public static void Main( )
    {
        // This array is to be modified and displayed.
        short[ ] arr = { 258, 259, 260, 261, 262, 263, 264, 
                         265, 266, 267, 268, 269, 270, 271 };

        Console.WriteLine( "This example of the Buffer class " +
            "methods generates the following output.\n" +
            "Note: The array is displayed from right to left.\n" );
        Console.WriteLine( "Initial values of array:\n" );

        // Display the initial array values and ByteLength.
        DisplayArray( arr );
        Console.WriteLine( "\nBuffer.ByteLength( arr ): {0}", 
            Buffer.ByteLength( arr ) );

        // Copy a region of the array; set a byte within the array.
        Console.WriteLine( "\nCall these methods: \n" +
            "  Buffer.BlockCopy( arr, 5, arr, 16, 9 ),\n" +
            "  Buffer.SetByte( arr, 7, 170 ).\n" );

        Buffer.BlockCopy( arr, 5, arr, 16, 9 );
        Buffer.SetByte( arr, 7, 170 );

        // Display the array and a byte within the array.
        Console.WriteLine( "Final values of array:\n" );
        DisplayArray( arr );
        Console.WriteLine( "\nBuffer.GetByte( arr, 26 ): {0}", 
            Buffer.GetByte( arr, 26 ) );
    }
}

/*
This example of the Buffer class methods generates the following output.
Note: The array is displayed from right to left.

Initial values of array:

  arr: 010F 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102

Buffer.ByteLength( arr ): 28

Call these methods:
  Buffer.BlockCopy( arr, 5, arr, 16, 9 ),
  Buffer.SetByte( arr, 7, 170 ).

Final values of array:

  arr: 010F 0101 0801 0701 0601 0501 0109 0108 0107 0106 AA05 0104 0103 0102

Buffer.GetByte( arr, 26 ): 15
*/

// Example of the Buffer class methods.
import System.*;

class BufferClassDemo
{
    // Display the array elements from right to left in hexadecimal.
    public static void DisplayArray(short arr[])
    {
        Console.Write("  arr:");
        for (int loopX = arr.get_Length() - 1; loopX >= 0; loopX--) {
            Console.Write(" {0:X4}", arr.get_Item(loopX));
        }
        Console.WriteLine();
    } //DisplayArray

    public static void main(String[] args)
    {
        // This array is to be modified and displayed.
        short arr[] = {    258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 
            269, 270, 271 };

        Console.WriteLine(("This example of the Buffer class "  
            + "methods generates the following output.\n" 
            + "Note: The array is displayed from right to left.\n"));
        Console.WriteLine("Initial values of array:\n");

        // Display the initial array values and ByteLength.
        DisplayArray(arr);
        Console.WriteLine("\nBuffer.ByteLength( arr ): {0}", 
            (Int32)Buffer.ByteLength(arr));

        // Copy a region of the array; set a byte within the array.
        Console.WriteLine(("\nCall these methods: \n" 
            + "  Buffer.BlockCopy( arr, 5, arr, 16, 9 ),\n" 
            + "  Buffer.SetByte( arr, 7, 170 ).\n"));
        Buffer.BlockCopy(arr, 5, arr, 16, 9);
        Buffer.SetByte(arr, 7, (ubyte)(170));

        // Display the array and a byte within the array.
        Console.WriteLine("Final values of array:\n");
        DisplayArray(arr);
        Console.WriteLine("\nBuffer.GetByte( arr, 26 ): {0}", 
            System.Convert.ToString(Buffer.GetByte(arr, 26)));
    } //main
} //BufferClassDemo

/*
This example of the Buffer class methods generates the following output.
Note: The array is displayed from right to left.

Initial values of array:

  arr: 010F 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102

Buffer.ByteLength( arr ): 28

Call these methods:
  Buffer.BlockCopy( arr, 5, arr, 16, 9 ),
  Buffer.SetByte( arr, 7, 170 ).

Final values of array:

  arr: 010F 0101 0801 0701 0601 0501 0109 0108 0107 0106 AA05 0104 0103 0102

Buffer.GetByte( arr, 26 ): 15
*/

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft