IntPtr.Subtraction(IntPtr, Int32) Opérateur
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Soustrait un décalage d’un entier signé.
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
Paramètres
- pointer
-
IntPtr
nativeint
Entier signé à partir duquel soustraire le décalage.
- offset
- Int32
Décalage à soustraire.
Retours
nativeint
Entier signé qui reflète la soustraction de offsetpointer.
Remarques
La Subtraction méthode définit l’opération de soustraction pour IntPtr les objets. Il active le code tel que le suivant.
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
Les langues qui ne prennent pas en charge les opérateurs personnalisés peuvent appeler la méthode à la Subtract place.
L’opération de soustraction ne lève pas d’exception si le résultat est trop petit pour représenter en tant qu’entier signé dans le processus en cours d’exécution. Au lieu de cela, elle est effectuée dans un contexte non vérifié.
En C# à partir de la version 11 et lorsque vous ciblez le runtime .NET 7 ou ultérieur, cette API est accessible uniquement via la réflexion. L’opérateur de soustraction est directement reconnu par la langue et suit le comportement de langage normal pour les opérations de soustraction, y compris le dépassement dans un checked contexte si le résultat est trop petit pour représenter.
La méthode équivalente pour cet opérateur est IntPtr.Subtract(IntPtr, Int32)