IntPtr.Subtraction(IntPtr, Int32) Operatore
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 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
Parametri
- pointer
-
IntPtr
nativeint
Intero con segno da cui sottrarre l'offset.
- offset
- Int32
Offset da sottrarre.
Valori restituiti
nativeint
Nuovo intero con segno che riflette la sottrazione di offset da pointer.
Commenti
Il Subtraction metodo definisce l'operazione di sottrazione per IntPtr gli oggetti . Abilita il codice, ad esempio il seguente.
int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
unsafe {
fixed(int* parr = &arr[arr.GetUpperBound(0)])
{
IntPtr ptr = new IntPtr(parr);
for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
{
IntPtr newPtr = ptr - ctr * sizeof(Int32);
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.Runtime.InteropServices
open FSharp.NativeInterop
[<EntryPoint>]
let main _ =
let arr =
[| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]
use parr = fixed &arr[arr.GetUpperBound 0]
let ptr = NativePtr.toNativeInt parr
for i = 0 to arr.GetUpperBound 0 do
let newPtr = ptr - nativeint i * nativeint sizeof<int>
printf $"{Marshal.ReadInt32 newPtr} "
0
// The example displays the following output:
// 20 18 16 14 12 10 8 6 4 2
Dim arr() As Integer = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }
Dim ptr As IntPtr = Marshal.UnsafeAddrOfPinnedArrayElement(arr, arr.GetUpperBound(0))
For ctr As Integer= 0 To arr.GetUpperBound(0)
Dim newPtr As IntPtr = ptr - ctr * Len(arr(0))
Console.Write("{0} ", Marshal.ReadInt32(newPtr))
Next
I linguaggi che non supportano operatori personalizzati possono invece chiamare il Subtract metodo .
L'operazione di sottrazione non genera un'eccezione se il risultato è troppo piccolo per rappresentare come intero con segno nel processo in esecuzione. Viene invece eseguita in un contesto deselezionato.
In C# a partire dalla versione 11 e quando la destinazione è il runtime .NET 7 o versione successiva, questa API è accessibile solo tramite reflection. L'operatore di sottrazione viene riconosciuto direttamente dalla lingua e seguirà il comportamento normale del linguaggio per le operazioni di sottrazione, inclusa l'overflow in un checked contesto se il risultato è troppo piccolo per rappresentare.
Il metodo equivalente per questo operatore è IntPtr.Subtract(IntPtr, Int32)