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

新的不帶正負號整數,反映 offset 加入 至 pointer

範例

下列範例會具現化指向十個元素陣列開頭的物件,然後呼叫 Add 方法來逐一 UIntPtr 查看陣列中的專案。

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 方法不會擲回例外狀況。 相反地,加法作業會在未核取的內容中執行。

不支援運算子多載或自訂運算子的語言可以使用這個方法,將位移新增至指標的值。

適用於

另請參閱