Evaluar y enviar comentarios

  Encender vista de ancho de banda bajo
Esta página es específica de
Microsoft Visual Studio 2008/.NET Framework 3.5

Hay además otras versiones disponibles para:
Biblioteca de clases de .NET Framework
Buffer..::.BlockCopy (Método)

Actualización: noviembre 2007

Copia un número especificado de bytes de una matriz de origen a partir de un desplazamiento determinado a una matriz de destino a partir de un desplazamiento determinado.

Espacio de nombres:  System
Ensamblado:  mscorlib (en mscorlib.dll)
Visual Basic (Declaración)
Public Shared Sub BlockCopy ( _
    src As Array, _
    srcOffset As Integer, _
    dst As Array, _
    dstOffset As Integer, _
    count As Integer _
)
Visual Basic (Uso)
Dim src As Array
Dim srcOffset As Integer
Dim dst As Array
Dim dstOffset As Integer
Dim count As Integer

Buffer.BlockCopy(src, srcOffset, dst, _
    dstOffset, count)
C#
public static void BlockCopy(
    Array src,
    int srcOffset,
    Array dst,
    int dstOffset,
    int count
)
Visual C++
public:
static void BlockCopy(
    Array^ src, 
    int srcOffset, 
    Array^ dst, 
    int dstOffset, 
    int count
)
J#
public static void BlockCopy(
    Array src,
    int srcOffset,
    Array dst,
    int dstOffset,
    int count
)
JScript
public static function BlockCopy(
    src : Array, 
    srcOffset : int, 
    dst : Array, 
    dstOffset : int, 
    count : int
)

Parámetros

src
Tipo: System..::.Array
Búfer de origen.
srcOffset
Tipo: System..::.Int32
Desplazamiento de bytes hacia src.
dst
Tipo: System..::.Array
Búfer de destino.
dstOffset
Tipo: System..::.Int32
Desplazamiento de bytes hacia dst.
count
Tipo: System..::.Int32
Número de bytes que se van a copiar.
ExcepciónCondición
ArgumentNullException

El valor de src o dst es nullNothingnullptrreferencia null (Nothing en Visual Basic).

ArgumentException

src o dst no es una matriz de primitivas.

-O bien-

La longitud de src es menor que srcOffset más count.

-O bien-

La longitud de dst es menor que dstOffset más count.

ArgumentOutOfRangeException

srcOffset, dstOffset, o count es menor que 0.

Copia bytes del parámetro count desde src, a partir de srcOffset, hasta dst, a partir de dstOffset.

Los desarrolladores deben tener el cuidado y no olvidar que el método BlockCopy obtiene acceso a la matriz de parámetros src utilizando los desplazamiento, no las construcciones de programación como índices ni los límites de la matriz superior e inferior. Por ejemplo, si en el lenguaje de programación de su aplicación declara una matriz Int32 con un límite inferior basado en cero de -50, después pasa la matriz y un desplazamiento de 5 al método BlockCopy, el primer elemento de matriz al que el método tendrá acceso es el segundo elemento de matriz que está en el índice -49. Además, el byte del elemento de la matriz -49 al que primero se tiene acceso depende del orden de bytes del equipo que ejecuta la aplicación.

El ejemplo de código siguiente copia regiones de matrices con el método BlockCopy.

Visual Basic
' Example of the Buffer.BlockCopy method.
Imports System
Imports Microsoft.VisualBasic

Module BlockCopyDemo

    ' Display the array contents in hexadecimal.
    Sub DisplayArray( arr As Array, name As String )

        ' Get the array element width; format the formatting string.
        Dim loopX     As Integer
        Dim elemWidth As Integer = _
            Buffer.ByteLength( arr ) / arr.Length
        Dim format    As String = _
            String.Format( " {{0:X{0}}}", 2 * elemWidth )

        ' Display the array elements from right to left.
        Console.Write( "{0,5}:", name )
        For loopX = arr.Length - 1 to 0 Step -1
            Console.Write( format, arr( loopX ) )
        Next loopX
        Console.WriteLine( )
    End Sub

    Sub Main( )

        ' These are source and destination arrays for BlockCopy.
        Dim src( )  As Short = { 258, 259, 260, 261, 262, 263, 264, _
                                 265, 266, 267, 268, 269, 270 }
        Dim dest( ) As Long  = { 17, 18, 19, 20 }

        Console.WriteLine( "This example of the " & vbCrLf & _
            "Buffer.BlockCopy( Array, Integer, Array, " & _
            "Integer, Integer ) " & vbCrLf & "method generates " & _
            "the following output." & vbCrLf & "Note: The " & _
            "arrays are displayed from right to left." & vbCrLf )
        Console.WriteLine( "  Initial values of arrays:" & vbCrLf )

        ' Display the values of the arrays.
        DisplayArray( src, "src" )
        DisplayArray( dest, "dest" )

        ' Copy two regions of source array to destination array,
        ' and two overlapped copies from source to source.
        Console.WriteLine( vbCrLf & _
            "  Call these methods: " & vbCrLf & vbCrLf & _
            " Buffer.BlockCopy( src, 5, dest, 7, 6 )," & vbCrLf & _
            " Buffer.BlockCopy( src, 16, dest, 22, 5 )," & vbCrLf & _
            "  (these overlap source and destination)" & vbCrLf & _
            " Buffer.BlockCopy( src, 4, src, 5, 7 )," & vbCrLf & _
            " Buffer.BlockCopy( src, 16, src, 15, 7 )." & vbCrLf )
        Console.WriteLine( "  Final values of arrays:" & vbCrLf )

        Buffer.BlockCopy( src, 5, dest, 7, 6 )
        Buffer.BlockCopy( src, 16, dest, 22, 5 )
        Buffer.BlockCopy( src, 4, src, 5, 7 )
        Buffer.BlockCopy( src, 16, src, 15, 7 )

        ' Display the arrays again.
        DisplayArray( src, "src" )
        DisplayArray( dest, "dest" )

    End Sub 
End Module 

' This example of the
' Buffer.BlockCopy( Array, Integer, Array, Integer, Integer )
' method generates the following output.
' Note: The arrays are displayed from right to left.
' 
'   Initial values of arrays:
' 
'   src: 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
'  dest: 0000000000000014 0000000000000013 0000000000000012 0000000000000011
' 
'   Call these methods:
' 
'  Buffer.BlockCopy( src, 5, dest, 7, 6 ),
'  Buffer.BlockCopy( src, 16, dest, 22, 5 ),
'   (these overlap source and destination)
'  Buffer.BlockCopy( src, 4, src, 5, 7 ),
'  Buffer.BlockCopy( src, 16, src, 15, 7 ).
' 
'   Final values of arrays:
' 
'   src: 010E 010D 0D01 0C01 0B01 0A09 0108 0701 0601 0501 0404 0103 0102
'  dest: 00000000000C010B 010A000000000013 0000000701060105 0100000000000011

C#
// Example of the Buffer.BlockCopy method.
using System;

class BlockCopyDemo
{
    // Display the array contents in hexadecimal.
    public static void DisplayArray( Array arr, string name )
    {
        // Get the array element width; format the formatting string.
        int elemWidth = Buffer.ByteLength( arr ) / arr.Length;
        string format = String.Format( " {{0:X{0}}}", 2 * elemWidth );

        // Display the array elements from right to left.
        Console.Write( "{0,5}:", name );
        for( int loopX = arr.Length - 1; loopX >= 0; loopX-- )
            Console.Write( format, arr.GetValue( loopX ) );
        Console.WriteLine( );
    }

    public static void Main( )
    {
        // These are source and destination arrays for BlockCopy.
        short[ ] src  = { 258, 259, 260, 261, 262, 263, 264, 
                          265, 266, 267, 268, 269, 270 };
        long[ ]  dest = { 17, 18, 19, 20 };

        Console.WriteLine( "This example of the \n" +
            "Buffer.BlockCopy( Array, int, Array, int, " +
            "int ) \nmethod generates the following output.\n" +
            "Note: The arrays are displayed from right to left.\n" );
        Console.WriteLine( "  Initial values of arrays:\r\n" );

        // Display the values of the arrays.
        DisplayArray( src, "src" );
        DisplayArray( dest, "dest" );

        // Copy two regions of source array to destination array,
        // and two overlapped copies from source to source.
        Console.WriteLine( "\n  Call these methods: \n\n" +
            " Buffer.BlockCopy( src, 5, dest, 7, 6 ),\n" +
            " Buffer.BlockCopy( src, 16, dest, 22, 5 ),\n" +
            "  (these overlap source and destination)\n" +
            " Buffer.BlockCopy( src, 4, src, 5, 7 ),\n" +
            " Buffer.BlockCopy( src, 16, src, 15, 7 ).\n" );
        Console.WriteLine( "  Final values of arrays:\n" );

        Buffer.BlockCopy( src, 5, dest, 7, 6 );
        Buffer.BlockCopy( src, 16, dest, 22, 5 );
        Buffer.BlockCopy( src, 4, src, 5, 7 );
        Buffer.BlockCopy( src, 16, src, 15, 7 );

        // Display the arrays again.
        DisplayArray( src, "src" );
        DisplayArray( dest, "dest" );
    }
}

/*
This example of the
Buffer.BlockCopy( Array, int, Array, int, int )
method generates the following output.
Note: The arrays are displayed from right to left.

  Initial values of arrays:

  src: 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
 dest: 0000000000000014 0000000000000013 0000000000000012 0000000000000011

  Call these methods:

 Buffer.BlockCopy( src, 5, dest, 7, 6 ),
 Buffer.BlockCopy( src, 16, dest, 22, 5 ),
  (these overlap source and destination)
 Buffer.BlockCopy( src, 4, src, 5, 7 ),
 Buffer.BlockCopy( src, 16, src, 15, 7 ).

  Final values of arrays:

  src: 010E 010D 0D01 0C01 0B01 0A09 0108 0701 0601 0501 0404 0103 0102
 dest: 00000000000C010B 010A000000000013 0000000701060105 0100000000000011
*/

Visual C++
// Example of the Buffer::BlockCopy method.
using namespace System;

// Display the array contents in hexadecimal.
void DisplayArray( Array^ arr, String^ name )
{

   // Get the array element width; format the formatting string.
   int elemWidth = Buffer::ByteLength( arr ) / arr->Length;
   String^ format = String::Format( " {{0:X{0}}}", 2 * elemWidth );

   // Display the array elements from right to left.
   Console::Write( "{0,5}:", name );
   for ( int loopX = arr->Length - 1; loopX >= 0; loopX-- )
      Console::Write( format, arr->GetValue( loopX ) );
   Console::WriteLine();
}

int main()
{

   // These are source and destination arrays for BlockCopy.
   array<short>^src = {258,259,260,261,262,263,264,265,266,267,268,269,270};
   array<__int64>^dest = {17,18,19,20};
   Console::WriteLine( "This example of the \n"
   "Buffer::BlockCopy( Array*, int, Array*, int, "
   "int ) \nmethod generates the following output.\n"
   "Note: The arrays are displayed from right to left.\n" );
   Console::WriteLine( "  Initial values of arrays:\r\n" );

   // Display the values of the arrays.
   DisplayArray( src, "src" );
   DisplayArray( dest, "dest" );

   // Copy two regions of source array to destination array,
   // and two overlapped copies from source to source.
   Console::WriteLine( "\n  Call these methods: \n\n"
   " Buffer::BlockCopy( src, 5, dest, 7, 6 ),\n"
   " Buffer::BlockCopy( src, 16, dest, 22, 5 ),\n"
   "  (these overlap source and destination)\n"
   " Buffer::BlockCopy( src, 4, src, 5, 7 ),\n"
   " Buffer::BlockCopy( src, 16, src, 15, 7 ).\n" );
   Console::WriteLine( "  Final values of arrays:\n" );
   Buffer::BlockCopy( src, 5, dest, 7, 6 );
   Buffer::BlockCopy( src, 16, dest, 22, 5 );
   Buffer::BlockCopy( src, 4, src, 5, 7 );
   Buffer::BlockCopy( src, 16, src, 15, 7 );

   // Display the arrays again.
   DisplayArray( src, "src" );
   DisplayArray( dest, "dest" );
}

/*
This example of the
Buffer::BlockCopy( Array*, int, Array*, int, int )
method generates the following output.
Note: The arrays are displayed from right to left.

  Initial values of arrays:

  src: 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
 dest: 0000000000000014 0000000000000013 0000000000000012 0000000000000011

  Call these methods:

 Buffer::BlockCopy( src, 5, dest, 7, 6 ),
 Buffer::BlockCopy( src, 16, dest, 22, 5 ),
  (these overlap source and destination)
 Buffer::BlockCopy( src, 4, src, 5, 7 ),
 Buffer::BlockCopy( src, 16, src, 15, 7 ).

  Final values of arrays:

  src: 010E 010D 0D01 0C01 0B01 0A09 0108 0701 0601 0501 0404 0103 0102
 dest: 00000000000C010B 010A000000000013 0000000701060105 0100000000000011
*/

J#
// Example of the Buffer.BlockCopy method.
import System.*;

class BlockCopyDemo
{
    // Display the array contents in hexadecimal.
    public static void DisplayArray(Array arr, String name)
    {
        // Get the array element width; format the formatting string.
        int elemWidth = Buffer.ByteLength(arr) / arr.get_Length();
        String format = String.Format(" {{0:X{0}}}", (Int32)(2 * elemWidth));

        // Display the array elements from right to left.
        Console.Write("{0,5}:", name);
        for (int loopX = arr.get_Length() - 1; loopX >= 0; loopX--) {
            Console.Write(format, arr.GetValue(loopX));
        }
        Console.WriteLine();
    } //DisplayArray

    public static void main(String[] args)
    {
        // These are source and destination arrays for BlockCopy.
        short src[] = { 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 
            269, 270 };
        long dest[] =  { 17, 18, 19, 20 };

        Console.WriteLine(("This example of the \n"  
            + "Buffer.BlockCopy( Array, int, Array, int, int ) \n"
            + "method generates the following output.\n" 
            + "Note: The arrays are displayed from right to left.\n"));
        Console.WriteLine("  Initial values of arrays:\r\n");

        // Display the values of the arrays.
        DisplayArray(src, "src");
        DisplayArray(dest, "dest");

        // Copy two regions of source array to destination array,
        // and two overlapped copies from source to source.
        Console.WriteLine("\n  Call these methods: \n\n"  
            + " Buffer.BlockCopy( src, 5, dest, 7, 6 ),\n" 
            + " Buffer.BlockCopy( src, 16, dest, 22, 5 ),\n"  
            + "  (these overlap source and destination)\n"  
            + " Buffer.BlockCopy( src, 4, src, 5, 7 ),\n"  
            + " Buffer.BlockCopy( src, 16, src, 15, 7 ).\n");
        Console.WriteLine("  Final values of arrays:\n");
        Buffer.BlockCopy(src, 5, dest, 7, 6);
        Buffer.BlockCopy(src, 16, dest, 22, 5);
        Buffer.BlockCopy(src, 4, src, 5, 7);
        Buffer.BlockCopy(src, 16, src, 15, 7);

        // Display the arrays again.
        DisplayArray(src, "src");
        DisplayArray(dest, "dest");
    } //main
} //BlockCopyDemo

/*
This example of the
Buffer.BlockCopy( Array, int, Array, int, int )
method generates the following output.
Note: The arrays are displayed from right to left.

  Initial values of arrays:

  src: 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
 dest: 0000000000000014 0000000000000013 0000000000000012 0000000000000011

  Call these methods:

 Buffer.BlockCopy( src, 5, dest, 7, 6 ),
 Buffer.BlockCopy( src, 16, dest, 22, 5 ),
  (these overlap source and destination)
 Buffer.BlockCopy( src, 4, src, 5, 7 ),
 Buffer.BlockCopy( src, 16, src, 15, 7 ).

  Final values of arrays:

  src: 010E 010D 0D01 0C01 0B01 0A09 0108 0701 0601 0501 0404 0103 0102
 dest: 00000000000C010B 010A000000000013 0000000701060105 0100000000000011
*/

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

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

.NET Framework

Compatible con: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 3.5, 2.0, 1.0

XNA Framework

Compatible con: 2.0, 1.0
Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2009 Microsoft Corporation. Reservados todos los derechos. Términos de uso  |  Marcas Registradas  |  Privacidad
Page view tracker