Unsafe.Add Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
Add<T>(Void*, Int32) |
Adds an element offset to the given unmanaged pointer. |
Add<T>(T, Int32) |
Adds an offset to the given managed pointer. |
Add<T>(T, IntPtr) |
Adds an element offset to the given managed pointer. |
Add<T>(T, UIntPtr) |
Adds an element offset to the given managed pointer. |
Add<T>(Void*, Int32)
- Source:
- Unsafe.cs
- Source:
- Unsafe.cs
- Source:
- Unsafe.cs
Important
This API is not CLS-compliant.
Adds an element offset to the given unmanaged pointer.
public:
generic <typename T>
static void* Add(void* source, int elementOffset);
public static void* Add<T> (void* source, int elementOffset);
[System.CLSCompliant(false)]
public static void* Add<T> (void* source, int elementOffset);
static member Add : nativeptr<unit> * int -> nativeptr<unit>
[<System.CLSCompliant(false)>]
static member Add : nativeptr<unit> * int -> nativeptr<unit>
Type Parameters
- T
The type whose size will be used as a scale factor for elementOffset
.
Parameters
- source
- Void*
The unmanaged pointer to add the offset to.
- elementOffset
- Int32
The offset to add.
Returns
A new unmanaged pointer that reflects the addition of the specified offset to the source pointer.
- Attributes
Remarks
The elementOffset
parameter is the number of T
-sized elements (not bytes) to add to the source
pointer. For example, given a source pointer ptr, the call Unsafe.Add<int>(ptr, 20)
will return a new pointer whose address points 80 bytes (= 20 elements * 4 bytes per element) beyond ptr.
If elementOffset
is a calculated value rather than a hardcoded literal, callers should consider the possibility of integer overflow. For example, in the call Unsafe.Add<int>(ptr, a * b)
, the caller must ensure the intermediate value a * b
does not overflow the bounds of Int32
.
Applies to
Add<T>(T, Int32)
- Source:
- Unsafe.cs
- Source:
- Unsafe.cs
- Source:
- Unsafe.cs
Adds an offset to the given managed pointer.
public:
generic <typename T>
static T % Add(T % source, int elementOffset);
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
Type Parameters
- T
The elemental type of the managed pointer.
Parameters
- source
- T
The managed pointer to add the offset to.
- elementOffset
- Int32
The offset to add.
Returns
A new managed pointer that reflects the addition of the specified offset to the source pointer.
Remarks
The elementOffset
parameter is the number of T
-sized elements (not bytes) to add to the source
pointer. For example, given a source pointer ptr of type ref int
, the call Unsafe.Add<int>(ref ptr, 20)
will return a new pointer whose address points 80 bytes (= 20 elements * 4 bytes per element) beyond ptr.
If elementOffset
is a calculated value rather than a hardcoded literal, callers should consider the possibility of integer overflow. For example, in the call Unsafe.Add<int>(ref ptr, a * b)
, the caller must ensure the intermediate value a * b
does not overflow the bounds of Int32
.
Applies to
Add<T>(T, IntPtr)
- Source:
- Unsafe.cs
- Source:
- Unsafe.cs
- Source:
- Unsafe.cs
Adds an element offset to the given managed pointer.
public:
generic <typename T>
static T % Add(T % source, IntPtr elementOffset);
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
Type Parameters
- T
The elemental type of the managed pointer.
Parameters
- source
- T
The managed pointer to add the offset to.
- elementOffset
-
IntPtr
nativeint
The offset to add.
Returns
A new managed pointer that reflects the addition of the specified offset to the source pointer.
Remarks
The elementOffset
parameter is the number of T
-sized elements (not bytes) to add to the source
pointer. For example, given a source pointer ptr of type ref int
, the call Unsafe.Add<int>(ref ptr, (nint)20)
will return a new pointer whose address points 80 bytes (= 20 elements * 4 bytes per element) beyond ptr.
If elementOffset
is a calculated value rather than a hardcoded literal, callers should consider the possibility of integer overflow. For example, in the call Unsafe.Add<int>(ref ptr, a * b)
, the caller must ensure the intermediate value a * b
does not overflow the bounds of IntPtr
.
Applies to
Add<T>(T, UIntPtr)
- Source:
- Unsafe.cs
- Source:
- Unsafe.cs
- Source:
- Unsafe.cs
Important
This API is not CLS-compliant.
Adds an element offset to the given managed pointer.
public:
generic <typename T>
static T % Add(T % source, UIntPtr elementOffset);
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);
static member Add : 'T * unativeint -> 'T
[<System.CLSCompliant(false)>]
static member Add : 'T * unativeint -> 'T
Public Shared Function Add(Of T) (ByRef source As T, elementOffset As UIntPtr) As T
Type Parameters
- T
The elemental type of the managed pointer.
Parameters
- source
- T
The managed pointer to add the offset to.
- elementOffset
-
UIntPtr
nuint
unativeint
The offset to add.
Returns
A new managed pointer that reflects the addition of the specified offset to the source pointer.
- Attributes
Remarks
The elementOffset
parameter is the number of T
-sized elements (not bytes) to add to the source
pointer. For example, given a source pointer ptr of type ref int
, the call Unsafe.Add<int>(ref ptr, (nuint)20)
will return a new pointer whose address points 80 bytes (= 20 elements * 4 bytes per element) beyond ptr.
If elementOffset
is a calculated value rather than a hardcoded literal, callers should consider the possibility of integer overflow. For example, in the call Unsafe.Add<int>(ref ptr, a * b)
, the caller must ensure the intermediate value a * b
does not overflow the bounds of UIntPtr
.