英語で読む

次の方法で共有


IntPtr.Add(IntPtr, Int32) メソッド

定義

符号付き整数にオフセットを追加します。

C#
public static IntPtr Add (IntPtr pointer, int offset);

パラメーター

pointer
IntPtr

オフセットを追加する符号付き整数。

offset
Int32

加算するオフセット。

戻り値

IntPtr

の加算offsetpointerを反映する新しい符号付き整数。

次の例では、10 要素配列の先頭を指すオブジェクトをインスタンス化 IntPtr し、メソッドを Add 呼び出して配列内の要素を反復処理します。

C#
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

注釈

結果が大きすぎて実行中のプロセスで符号付き整数として表現できない場合、メソッドは Add 例外をスローしません。 代わりに、追加操作はオフのコンテキストで実行されます。

演算子のオーバーロードまたはカスタム演算子をサポートしていない言語では、このメソッドを使用してポインターの値にオフセットを追加できます。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください