Unsafe.Subtract Method

Definition

Overloads

Subtract<T>(Void*, Int32)

Subtracts an element offset from the given unmanaged pointer.

Subtract<T>(T, Int32)

Subtracts an offset from the given managed pointer.

Subtract<T>(T, IntPtr)

Subtracts an element offset from the given managed pointer.

Subtract<T>(T, UIntPtr)

Subtracts an element offset from the given managed pointer.

Subtract<T>(Void*, Int32)

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

Important

This API is not CLS-compliant.

Subtracts an element offset from the given unmanaged pointer.

public:
generic <typename T>
 static void* Subtract(void* source, int elementOffset);
public static void* Subtract<T> (void* source, int elementOffset);
[System.CLSCompliant(false)]
public static void* Subtract<T> (void* source, int elementOffset);
static member Subtract : nativeptr<unit> * int -> nativeptr<unit>
[<System.CLSCompliant(false)>]
static member Subtract : 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 subtract the offset from.

elementOffset
Int32

The offset to subtract.

Returns

Void*

A new unmanaged pointer that reflects the subtraction of the specified offset from the source pointer.

Attributes

Remarks

The elementOffset parameter is the number of T-sized elements (not bytes) to remove from the source pointer. For example, given a source pointer ptr, the call Unsafe.Subtract<int>(ptr, 20) will return a new pointer whose address points 80 bytes (= 20 elements * 4 bytes per element) before 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.Subtract<int>(ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of Int32.

Applies to

Subtract<T>(T, Int32)

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

Subtracts an offset from the given managed pointer.

public:
generic <typename T>
 static T % Subtract(T % source, int elementOffset);
public static ref T Subtract<T> (ref T source, int elementOffset);
static member Subtract : 'T * int -> 'T
Public Shared Function Subtract(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 subtract the offset from.

elementOffset
Int32

The offset to subtract.

Returns

T

A new managed pointer that reflects the subtraction of the specified offset from the source pointer.

Remarks

The elementOffset parameter is the number of T-sized elements (not bytes) to subtract from the source pointer. For example, given a source pointer ptr of type ref int, the call Unsafe.Subtract<int>(ref ptr, 20) will return a new pointer whose address points 80 bytes (= 20 elements * 4 bytes per element) before 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.Subtract<int>(ref ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of Int32.

Applies to

Subtract<T>(T, IntPtr)

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

Subtracts an element offset from the given managed pointer.

public:
generic <typename T>
 static T % Subtract(T % source, IntPtr elementOffset);
public static ref T Subtract<T> (ref T source, IntPtr elementOffset);
static member Subtract : 'T * nativeint -> 'T
Public Shared Function Subtract(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 subtract the offset from.

elementOffset
IntPtr

nativeint

The offset to subtract.

Returns

T

A new managed pointer that reflects the subtraction of the specified offset from the source pointer.

Remarks

The elementOffset parameter is the number of T-sized elements (not bytes) to subtract from the source pointer. For example, given a source pointer ptr of type ref int, the call Unsafe.Subtract<int>(ref ptr, (nint)20) will return a new pointer whose address points 80 bytes (= 20 elements * 4 bytes per element) before 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.Subtract<int>(ref ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of IntPtr.

Applies to

Subtract<T>(T, UIntPtr)

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

Important

This API is not CLS-compliant.

Subtracts an element offset from the given managed pointer.

public:
generic <typename T>
 static T % Subtract(T % source, UIntPtr elementOffset);
public static ref T Subtract<T> (ref T source, nuint elementOffset);
[System.CLSCompliant(false)]
public static ref T Subtract<T> (ref T source, UIntPtr elementOffset);
static member Subtract : 'T * unativeint -> 'T
[<System.CLSCompliant(false)>]
static member Subtract : 'T * unativeint -> 'T
Public Shared Function Subtract(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 subtract the offset from.

elementOffset
UIntPtr

nuint

unativeint

The offset to subtract.

Returns

T

A new managed pointer that reflects the subtraction of the specified offset from the source pointer.

Attributes

Remarks

The elementOffset parameter is the number of T-sized elements (not bytes) to subtract from the source pointer. For example, given a source pointer ptr of type ref int, the call Unsafe.Subtract<int>(ref ptr, (nuint)20) will return a new pointer whose address points 80 bytes (= 20 elements * 4 bytes per element) before 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.Subtract<int>(ref ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of UIntPtr.

Applies to