Definition

Suma un desplazamiento al valor de un puntero.Adds an offset to the value of a pointer.

``````public:
static IntPtr Add(IntPtr pointer, int offset);``````
``public static IntPtr Add (IntPtr pointer, int offset);``
``static member Add : nativeint * int -> nativeint``
``Public Shared Function Add (pointer As IntPtr, offset As Integer) As IntPtr``

Parameters

pointer
IntPtr

Puntero al que se va a sumar el desplazamiento.The pointer to add the offset to.

offset
Int32

Desplazamiento que se va a sumar.The offset to add.

Returns

IntPtr

Nuevo puntero que es el resultado de sumar `offset` a `pointer`.A new pointer that reflects the addition of `offset` to `pointer`.

Examples

En el ejemplo siguiente se crea una instancia de un objeto IntPtr que apunta al principio de una matriz de diez elementos y, a continuación, llama al método Add para recorrer en iteración los elementos de la matriz.The following example instantiates an IntPtr object that points to the beginning of a ten-element array, and then calls the Add method to iterate the elements in the array.

``````using System;
using System.Runtime.InteropServices;

public class Example
{
public static void Main()
{
int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
unsafe {
fixed(int* parr = arr) {
IntPtr ptr = new IntPtr(parr);
// Get the size of an array element.
int size = sizeof(int);
for (int ctr = 0; ctr < arr.Length; ctr++)
{
IntPtr newPtr = IntPtr.Add(ptr, ctr * size);
}
}
}
}
}
// The example displays the following output:
//       2   4   6   8   10   12   14   16   18   20
``````
``````Imports System.Runtime.InteropServices

Module Example
Public Sub Main()
Dim arr() As Integer = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }
Dim ptr As IntPtr = Marshal.UnsafeAddrOfPinnedArrayElement(arr, 0)

For ctr As Integer= 0 To arr.Length - 1
Dim newPtr As IntPtr = IntPtr.Add(ptr, ctr * Len(arr(0)))
Next
End Sub
End Module
' The example displays the following output:
'       2   4   6   8   10   12   14   16   18   20
``````

Remarks

El método Add no inicia una excepción si el resultado es demasiado grande para representarse como un puntero en la plataforma especificada.The Add method does not throw an exception if the result is too large to represent as a pointer on the specified platform. En su lugar, la operación de suma se realiza en un contexto no comprobado.Instead, the addition operation is performed in an unchecked context.

Los lenguajes que no admiten la sobrecarga de operadores o los operadores personalizados pueden utilizar este método para agregar un desplazamiento al valor de un puntero.Languages that do not support operator overloading or custom operators can use this method to add an offset to the value of a pointer.