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.

Примеры

В следующем примере создается 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 не создает исключение, если результат слишком велик для представления в виде целого числа без знака в выполняемом процессе. Вместо этого операция сложения выполняется в контексте без флажка.

Языки, которые не поддерживают перегрузку операторов или пользовательские операторы, могут использовать этот метод для добавления смещения к значению указателя.

Применяется к

См. также раздел