语言

Unsafe.Add 方法

定义

重载

名称 说明
Add<T>(Void*, Int32)

向给定的非托管指针添加元素偏移量。

Add<T>(T, Int32)

向给定的托管指针添加偏移量。

Add<T>(T, IntPtr)

向给定的托管指针添加元素偏移量。

Add<T>(T, UIntPtr)

向给定的托管指针添加元素偏移量。

Add<T>(Void*, Int32)

Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs

重要

此 API 不符合 CLS。

向给定的非托管指针添加元素偏移量。

public:
generic <typename T>
 static void* Add(void* source, int elementOffset);
[System.CLSCompliant(false)]
public static void* Add<T>(void* source, int elementOffset) where T : allows ref struct;
public static void* Add<T>(void* source, int elementOffset);
[System.CLSCompliant(false)]
public static void* Add<T>(void* source, int elementOffset);
[<System.CLSCompliant(false)>]
static member Add : nativeptr<unit> * int -> nativeptr<unit>
static member Add : nativeptr<unit> * int -> nativeptr<unit>

类型参数

T

其大小将用作其大小的刻度因子的类型 elementOffset。

参数

source
Void*

要向其添加偏移量的非托管指针。

elementOffset
Int32

要添加的偏移量。

返回

Void*

一个新的非托管指针,反映向源指针添加指定的偏移量。

特性

注解

参数elementOffset是要添加到source指针的 T-sized 元素(而不是字节)的数目。 例如,给定源指针 ptr,调用 Unsafe.Add<int>(ptr, 20) 将返回一个新指针,其地址点为 80 字节(= 20 个元素 * 每个元素 4 个字节)超出 ptr。

如果 elementOffset 计算值而不是硬编码文本,则调用方应考虑整数溢出的可能性。 例如,在调用 Unsafe.Add<int>(ptr, a * b)中,调用方必须确保中间值 a * b 不会溢出边界 Int32。

适用于

Add<T>(T, Int32)

Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs

向给定的托管指针添加偏移量。

public:
generic <typename T>
 static T % Add(T % source, int elementOffset);
public static ref T Add<T>(ref T source, int elementOffset) where T : allows ref struct;
public static ref T Add<T>(ref T source, int elementOffset);
static member Add : 'T * int -> 'T
Public Shared Function Add(Of T) (ByRef source As T, elementOffset As Integer) As T

类型参数

T

托管指针的元素类型。

参数

source
T

要向其添加偏移量的托管指针。

elementOffset
Int32

要添加的偏移量。

返回

T

一个新的托管指针,反映向源指针添加指定的偏移量。

注解

参数elementOffset是要添加到source指针的 T-sized 元素(而不是字节)的数目。 例如,给定类型的ref int源指针 ptr,调用Unsafe.Add<int>(ref ptr, 20)将返回一个新的指针,其地址点为 80 字节(= 20 个元素 * 每个元素 4 个字节)超出 ptr。

如果 elementOffset 计算值而不是硬编码文本,则调用方应考虑整数溢出的可能性。 例如,在调用 Unsafe.Add<int>(ref ptr, a * b)中,调用方必须确保中间值 a * b 不会溢出边界 Int32。

适用于

Add<T>(T, IntPtr)

Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs

向给定的托管指针添加元素偏移量。

public:
generic <typename T>
 static T % Add(T % source, IntPtr elementOffset);
public static ref T Add<T>(ref T source, IntPtr elementOffset) where T : allows ref struct;
public static ref T Add<T>(ref T source, IntPtr elementOffset);
static member Add : 'T * nativeint -> 'T
Public Shared Function Add(Of T) (ByRef source As T, elementOffset As IntPtr) As T

类型参数

T

托管指针的元素类型。

参数

source
T

要向其添加偏移量的托管指针。

elementOffset
IntPtr

nativeint

要添加的偏移量。

返回

T

一个新的托管指针,反映向源指针添加指定的偏移量。

注解

参数elementOffset是要添加到source指针的 T-sized 元素(而不是字节)的数目。 例如,给定类型的ref int源指针 ptr,调用Unsafe.Add<int>(ref ptr, (nint)20)将返回一个新的指针,其地址点为 80 字节(= 20 个元素 * 每个元素 4 个字节)超出 ptr。

如果 elementOffset 计算值而不是硬编码文本,则调用方应考虑整数溢出的可能性。 例如,在调用 Unsafe.Add<int>(ref ptr, a * b)中,调用方必须确保中间值 a * b 不会溢出边界 IntPtr。

适用于

Add<T>(T, UIntPtr)

Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs
Source:
Unsafe.cs

重要

此 API 不符合 CLS。

向给定的托管指针添加元素偏移量。

public:
generic <typename T>
 static T % Add(T % source, UIntPtr elementOffset);
[System.CLSCompliant(false)]
public static ref T Add<T>(ref T source, UIntPtr elementOffset) where T : allows ref struct;
public static ref T Add<T>(ref T source, nuint elementOffset);
[System.CLSCompliant(false)]
public static ref T Add<T>(ref T source, UIntPtr elementOffset);
[<System.CLSCompliant(false)>]
static member Add : 'T * unativeint -> 'T
static member Add : 'T * unativeint -> 'T
Public Shared Function Add(Of T) (ByRef source As T, elementOffset As UIntPtr) As T

类型参数

T

托管指针的元素类型。

参数

source
T

要向其添加偏移量的托管指针。

elementOffset
UIntPtr

nuint

unativeint

要添加的偏移量。

返回

T

一个新的托管指针,反映向源指针添加指定的偏移量。

特性

注解

参数elementOffset是要添加到source指针的 T-sized 元素(而不是字节)的数目。 例如,给定类型的ref int源指针 ptr,调用Unsafe.Add<int>(ref ptr, (nuint)20)将返回一个新的指针,其地址点为 80 字节(= 20 个元素 * 每个元素 4 个字节)超出 ptr。

如果 elementOffset 计算值而不是硬编码文本,则调用方应考虑整数溢出的可能性。 例如,在调用 Unsafe.Add<int>(ref ptr, a * b)中,调用方必须确保中间值 a * b 不会溢出边界 UIntPtr。

适用于