IntPtr.Add(IntPtr, Int32) 메서드

정의

부 서명된 정수에 오프셋을 추가합니다.

public:
 static IntPtr Add(IntPtr pointer, int offset);
public static IntPtr Add (IntPtr pointer, int offset);
static member Add : nativeint * int -> nativeint
Public Shared Function Add (pointer As IntPtr, offset As Integer) As IntPtr

매개 변수

pointer
IntPtr

nativeint

오프셋을 추가할 부록 정수입니다.

offset
Int32

더할 오프셋입니다.

반환

IntPtr

nativeint

에 추가 offset pointer된 것을 반영하는 새 부가 정수입니다.

예제

다음 예제에서는 10개 요소 배열의 시작을 가리키는 개체를 인스턴스화 IntPtr 한 다음, 메서드를 호출 Add 하여 배열의 요소를 반복합니다.

using System;
using System.Runtime.InteropServices;

public class Example
{
   public static void Main()
   {
      int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
      unsafe {
         fixed(int* parr = arr) {
            IntPtr ptr = new IntPtr(parr);
            // Get the size of an array element.
            int size = sizeof(int);
            for (int ctr = 0; ctr < arr.Length; ctr++)
            {
               IntPtr newPtr = IntPtr.Add(ptr, ctr * size);
               Console.Write("{0}   ", Marshal.ReadInt32(newPtr));
            }
         }
      }
   }
}
// The example displays the following output:
//       2   4   6   8   10   12   14   16   18   20
#nowarn "9"
open System
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
    let mutable arr = 
        [| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]
    
    use parr = fixed arr
    
    let ptr = NativePtr.toNativeInt parr

    // Get the size of an array element.
    let size = sizeof<int>
    for i = 0 to arr.Length - 1 do
        let newPtr = IntPtr.Add(ptr, i * size)
        printf $"{Marshal.ReadInt32 newPtr}   "
    0

// The example displays the following output:
//       2   4   6   8   10   12   14   16   18   20
Imports System.Runtime.InteropServices

Module Example
   Public Sub Main()
      Dim arr() As Integer = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }
      Dim ptr As IntPtr = Marshal.UnsafeAddrOfPinnedArrayElement(arr, 0)

      For ctr As Integer= 0 To arr.Length - 1
         Dim newPtr As IntPtr = IntPtr.Add(ptr, ctr * Len(arr(0)))
         Console.Write("{0}   ", Marshal.ReadInt32(newPtr))
      Next
   End Sub
End Module
' The example displays the following output:
'       2   4   6   8   10   12   14   16   18   20

설명

Add 결과가 너무 커서 실행 프로세스에서 부 서명된 정수로 나타낼 수 없는 경우 메서드는 예외를 throw하지 않습니다. 대신 선택되지 않은 컨텍스트에서 추가 작업이 수행됩니다.

연산자 오버로드 또는 사용자 지정 연산자를 지원하지 않는 언어는 이 메서드를 사용하여 포인터 값에 오프셋을 추가할 수 있습니다.

적용 대상

추가 정보