IntPtr.Addition(IntPtr, Int32) 运算符  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向有符号整数添加偏移量。
public:
 static IntPtr operator +(IntPtr pointer, int offset);public static IntPtr operator + (IntPtr pointer, int offset);static member ( + ) : nativeint * int -> nativeintPublic Shared Operator + (pointer As IntPtr, offset As Integer) As IntPtr参数
- pointer
- 
				
				IntPtr
nativeint 
要向其添加偏移量的带符号整数。
- offset
- Int32
要增加的偏移量。
返回
- 
			
			IntPtr
nativeint 
一个新的带符号整数,反映添加到 offset pointer。
注解
该方法 Addition 定义对象的 IntPtr 加法操作。 它启用以下代码。
int[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
unsafe {
   fixed(int* parr = arr)
   {
       IntPtr ptr = new IntPtr(parr);
       for (int ctr = 0; ctr < arr.Length; ctr++)
       {
          IntPtr newPtr = ptr + ctr * sizeof(Int32);
          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.Runtime.InteropServices
open FSharp.NativeInterop
[<EntryPoint>]
let main _ =
    let arr =
        [| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]
        
    use parr = fixed arr
    let ptr = NativePtr.toNativeInt parr
    for i = 0 to arr.Length - 1 do
        let newPtr = ptr + nativeint i * nativeint sizeof<int>
        printf $"{Marshal.ReadInt32 newPtr}   "
    0
    // The example displays the following output:
    //       2   4   6   8   10   12   14   16   18   20
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 = ptr + ctr * Len(arr(0))
   Console.WriteLine("{0}   ", Marshal.ReadInt32(newPtr))
Next
' The example displays the following output:
'       2   4   6   8   10   12   14   16   18   20
不支持自定义运算符的语言可以改为调用该方法 Add 。
如果结果太大而无法表示为执行过程中的带符号整数,则加法操作不会引发异常。 而是在未选中的上下文中执行。
从版本 11 开始的 C# 和面向 .NET 7 或更高版本运行时时,只能通过反射访问此 API。 加法运算符由语言直接识别,并且将遵循用于添加操作的正常语言行为,包括在结果太大而无法表示的情况下溢出 checked 上下文中。
此运算符的等效方法为 IntPtr.Add(IntPtr, Int32)