UIntPtr.Add(UIntPtr, Int32) 方法

定义

向无符号整数添加偏移量。

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

参数

pointer
UIntPtr

unativeint

要向其添加偏移量的无符号整数。

offset
Int32

要添加的偏移量。

返回

UIntPtr

unativeint

一个新的无符号整数,反映添加到 offsetpointer

示例

以下示例实例化一个 UIntPtr 指向十个元素数组开头的对象,然后调用 Add 该方法来循环访问数组中的元素。

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[0];
      for (int ctr = 0; ctr < arr.Length; ctr++)
      {
         UIntPtr newPtr = UIntPtr.Add(ptr, ctr);
         Console.Write("{0}   ", newPtr);
      }      
   }
}
// The example displays the following output:
//       1   2   3   4   5   6   7   8   9   10
open System

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

注解

如果结果过大而无法在执行过程中表示为无符号整数,则 Add 该方法不会引发异常。 相反,添加操作是在未选中的上下文中执行的。

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

适用于

另请参阅