UIntPtr.Subtract(UIntPtr, Int32) 方法

定义

从无符号整数中减去偏移量。

public:
 static UIntPtr Subtract(UIntPtr pointer, int offset);
public static UIntPtr Subtract(UIntPtr pointer, int offset);
static member Subtract : unativeint * int -> unativeint
Public Shared Function Subtract (pointer As UIntPtr, offset As Integer) As UIntPtr

参数

pointer
UIntPtr

unativeint

要从中减去偏移量的无符号整数。

offset
Int32

要减去的偏移量。

返回

UIntPtr

unativeint

一个新的无符号整数,反映从offset中减法pointer

示例

以下示例实例化一个 IntPtr 指向十个元素数组末尾的对象,然后调用 Subtract 该方法以相反的顺序循环访问数组中的元素。

using System;

public class Example
{
   public static void Main()
   {
      int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      UIntPtr ptr = (UIntPtr) arr[arr.GetUpperBound(0)];
      for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
      {
         UIntPtr newPtr = UIntPtr.Subtract(ptr, ctr);
         Console.Write("{0}   ", newPtr);
      }
   }
}
// The example displays the following output:
//       10   9   8   7   6   5   4   3   2   1
open System

let arr = [| 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 |]
let ptr = UIntPtr(uint arr[arr.GetUpperBound 0])
for i = 0 to arr.GetUpperBound 0 do
    let newPtr = UIntPtr.Subtract(ptr, i)
    printf $"{newPtr}   "
// The example displays the following output:
//       10   9   8   7   6   5   4   3   2   1
Module Example
   Public Sub Main()
      Dim arr() As Integer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
      Dim ptr As UIntPtr = CType(arr(arr.GetUpperBound(0)), UIntPtr)
      For ctr As Integer= 0 To arr.GetUpperBound(0)
         Dim newPtr As UIntPtr = UIntPtr.Subtract(ptr, ctr)
         Console.Write("{0}   ", newPtr)
      Next
   End Sub
End Module
' The example displays the following output:
'       10   9   8   7   6   5   4   3   2   1

注解

Subtract如果结果太小,无法在执行过程中表示为无符号整数,则该方法不会引发异常。 相反,减法运算是在未选中的上下文中执行的。

不支持运算符重载或自定义运算符的语言可以使用此方法从指针的值中减去偏移量。

适用于

另请参阅