Buffer 类

操作基元类型的数组。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public NotInheritable Class Buffer
用法
可对静态类的成员直接进行访问,无需类的实例。
[ComVisibleAttribute(true)] 
public static class Buffer
[ComVisibleAttribute(true)] 
public ref class Buffer abstract sealed
/** @attribute ComVisibleAttribute(true) */ 
public final class Buffer
ComVisibleAttribute(true) 
public final class Buffer

备注

Buffer 只影响基元类型的数组;此类不适用于对象。不管与基元类型关联的任何行为或限制如何,每个基元类型都被视为一系列字节。

Buffer 提供了许多方法,可以将字节从一个基元类型数组复制到另一个基元类型数组、从数组获取字节、在数组中设置字节,以及获取数组长度。对于操作基元类型,此类可比 System.Array 类中相似的方法提供更好的性能。

Buffer 适用于下列基元类型:BooleanCharSByteByteInt16UInt16Int32UInt32Int64UInt64IntPtrUIntPtrSingleDouble

示例

下面的代码示例演示多个 Buffer 类方法的用法。

' Example of the Buffer class methods.
Imports System
Imports Microsoft.VisualBasic

Module BufferClassDemo

    ' Display the array elements from right to left in hexadecimal.
    Sub DisplayArray( arr( ) As Short )

        Console.Write( "  arr:" )
        Dim loopX     As Integer
        For loopX = arr.Length - 1 To 0 Step -1
            Console.Write( " {0:X4}", arr( loopX ) )
        Next loopX
        Console.WriteLine( )
    End Sub

    Sub Main( )

        ' This array is to be modified and displayed.
        Dim arr( ) As Short = { 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." & vbCrLf & "Note: The " & _
            "array is displayed from right to left." & vbCrLf )
        Console.WriteLine( "Initial values of array:" & vbCrLf )

        ' Display the initial array values and ByteLength.
        DisplayArray( arr )
        Console.WriteLine( vbCrLf & _
            "Buffer.ByteLength( arr ): {0}", _
            Buffer.ByteLength( arr ) )

        ' Copy a region of the array; set a byte within the array.
        Console.WriteLine( vbCrLf & _
            "Call these methods: " & vbCrLf & _
            "  Buffer.BlockCopy( arr, 5, arr, 16, 9 )," & vbCrLf & _
            "  Buffer.SetByte( arr, 7, 170 )." & vbCrLf )

        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:" & vbCrLf )
        DisplayArray( arr )
        Console.WriteLine( vbCrLf & _
            "Buffer.GetByte( arr, 26 ): {0}", _
            Buffer.GetByte( arr, 26 ) )
    End Sub 
End Module 

' 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.
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.
using namespace System;

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

int main()
{
   
   // This array is to be modified and displayed.
   array<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
*/

继承层次结构

System.Object
  System.Buffer

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Buffer 成员
System 命名空间