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.

適用於

另請參閱