UIntPtr.Add(UIntPtr, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將位移加入至不帶正負號的整數。
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
一個新的無符號整數,反映將 加 offset 到 pointer。
範例
以下範例實例化一個 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 方法不會拋出例外。 相反地,加法運算是在未檢查的上下文中執行的。
不支援運算子重載或自訂運算子的語言,可以使用此方法為指標值增加偏移量。