IntPtr.Subtract(IntPtr, Int32) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Sottrae un offset da un intero con segno.
public:
static IntPtr Subtract(IntPtr pointer, int offset);
public static IntPtr Subtract (IntPtr pointer, int offset);
static member Subtract : nativeint * int -> nativeint
Public Shared Function Subtract (pointer As IntPtr, offset As Integer) As IntPtr
Parametri
- pointer
-
IntPtr
nativeint
Intero con segno da cui sottrarre l'offset.
- offset
- Int32
Offset da sottrarre.
Restituisce
-
IntPtr
nativeint
Nuovo intero con segno che riflette la sottrazione di offset da pointer.
Esempio
Nell'esempio seguente viene creata un'istanza di un IntPtr oggetto che punta alla fine di una matrice di dieci elementi e quindi chiama il Subtract metodo per scorrere gli elementi nella matrice in ordine inverso.
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};
// Get the size of a single array element.
int size = sizeof(int);
unsafe {
fixed(int* pend = &arr[arr.GetUpperBound(0)]) {
IntPtr ptr = new IntPtr(pend);
for (int ctr = 0; ctr < arr.Length; ctr++)
{
IntPtr newPtr = IntPtr.Subtract(ptr, ctr * size);
Console.Write("{0} ", Marshal.ReadInt32(newPtr));
}
}
}
}
}
// The example displays the following output:
// 20 18 16 14 12 10 8 6 4 2
#nowarn "9"
open System
open System.Runtime.InteropServices
open FSharp.NativeInterop
[<EntryPoint>]
let main _ =
let arr =
[| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]
// Get the size of a single array element.
let size = sizeof<int>
use pend = fixed &arr[arr.GetUpperBound 0]
let ptr = NativePtr.toNativeInt pend
for i = 0 to arr.Length - 1 do
let newPtr = IntPtr.Subtract(ptr, i * size)
printf $"{Marshal.ReadInt32 newPtr} "
0
// The example displays the following output:
// 20 18 16 14 12 10 8 6 4 2
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, arr.Length - 1)
Dim size As Integer = Len(arr(0))
For ctr As Integer = 0 To arr.Length - 1
Dim newPtr As IntPtr = IntPtr.Subtract(ptr, ctr * size)
Console.Write("{0} ", Marshal.ReadInt32(newPtr))
Next
End Sub
End Module
' The example displays the following output:
' 20 18 16 14 12 10 8 6 4 2
Commenti
Il Subtract metodo non genera un'eccezione se il risultato è troppo piccolo per rappresentare come intero con segno nel processo in esecuzione. Al contrario, l'operazione di sottrazione viene eseguita in un contesto deselezionato.
I linguaggi che non supportano l'overload degli operatori o gli operatori personalizzati possono utilizzare questo metodo per sottrarre un offset dal valore di un puntatore.