UIntPtr.Add(UIntPtr, Int32) 方法
定义
为无符号指针的值增加偏移量。Adds an offset to the value of an unsigned pointer.
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
要为其增加偏移量的无符号指针。The unsigned pointer to add the offset to.
- offset
- Int32
要增加的偏移量。The offset to add.
返回
新的无符号指针,反映向 offset 增加 pointer 的结果。A new unsigned pointer that reflects the addition of offset to pointer.
示例
下面的示例实例化一个 UIntPtr 指向十元素数组开头的对象,然后调用 Add 方法来循环访问数组中的元素。The following example instantiates a UIntPtr object that points to the beginning of a ten-element array, and then calls the Add method to iterate the elements in the array.
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
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如果结果太大而无法表示为指定平台上的指针,则此方法不会引发异常。The Add method does not throw an exception if the result is too large to represent as a pointer on the specified platform. 相反,在未检查的上下文中执行加法运算。Instead, the addition operation is performed in an unchecked context.
不支持运算符重载或自定义运算符的语言可以使用此方法将偏移量添加到无符号指针的值。Languages that do not support operator overloading or custom operators can use this method to add an offset to the value of an unsigned pointer.