IntPtr.Addition(IntPtr, Int32) Operator

Definicja

Dodaje przesunięcie do podpisanej liczby całkowitej.

public:
 static IntPtr operator +(IntPtr pointer, int offset);
public static IntPtr operator + (IntPtr pointer, int offset);
static member ( + ) : nativeint * int -> nativeint
Public Shared Operator + (pointer As IntPtr, offset As Integer) As IntPtr

Parametry

pointer
IntPtr

nativeint

Podpisana liczba całkowita w celu dodania przesunięcia do.

offset
Int32

Przesunięcie do dodania.

Zwraca

IntPtr

nativeint

Nowa podpisana liczba całkowita, która odzwierciedla dodanie offset elementu do pointerelementu .

Uwagi

Metoda Addition definiuje operację dodawania obiektów IntPtr . Umożliwia on kod, taki jak poniższy.

int[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
unsafe {
   fixed(int* parr = arr)
   {
       IntPtr ptr = new IntPtr(parr);
       for (int ctr = 0; ctr < arr.Length; ctr++)
       {
          IntPtr newPtr = ptr + ctr * sizeof(Int32);
          Console.Write("{0}   ", Marshal.ReadInt32(newPtr));
       }
   }
}
// The example displays the following output:
//       2   4   6   8   10   12   14   16   18   20
#nowarn "9"
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
    let arr =
        [| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]
        
    use parr = fixed arr

    let ptr = NativePtr.toNativeInt parr

    for i = 0 to arr.Length - 1 do
        let newPtr = ptr + nativeint i * nativeint sizeof<int>
        printf $"{Marshal.ReadInt32 newPtr}   "
    0

    // The example displays the following output:
    //       2   4   6   8   10   12   14   16   18   20
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 = ptr + ctr * Len(arr(0))
   Console.WriteLine("{0}   ", Marshal.ReadInt32(newPtr))
Next
' The example displays the following output:
'       2   4   6   8   10   12   14   16   18   20

Języki, które nie obsługują operatorów niestandardowych, mogą zamiast tego wywoływać metodę Add .

Operacja dodawania nie zgłasza wyjątku, jeśli wynik jest zbyt duży do reprezentowania jako podpisana liczba całkowita w procesie wykonywania. Zamiast tego jest wykonywany w nieznakowanym kontekście.

W języku C# począwszy od wersji 11 i w przypadku określania wartości docelowej środowiska uruchomieniowego .NET 7 lub nowszego ten interfejs API jest dostępny tylko za pośrednictwem odbicia. Operator dodawania jest rozpoznawany bezpośrednio przez język i będzie postępować zgodnie z normalnym zachowaniem checked języka na potrzeby operacji dodawania, w tym przepełnieniem w kontekście, jeśli wynik jest zbyt duży do reprezentowania.

Równoważna metoda dla tego operatora to IntPtr.Add(IntPtr, Int32)

Dotyczy

Zobacz też