Language

Unsafe.Subtract Method

Definition

Overloads

Name Description
Subtract(Void*, Int32)

Subtracts an element offset from the given unmanaged pointer.

Subtract(T, Int32)

Subtracts an offset from the given managed pointer.

Subtract(T, IntPtr)

Subtracts an element offset from the given managed pointer.

Subtract(T, UIntPtr)

Subtracts an element offset from the given managed pointer.

Subtract(Void*, Int32)

Source:
Unsafe.cs
Source:
Unsafe.cs
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 
 static void* Subtract(void* source, int elementOffset);
[System.CLSCompliant(false)]
public static void* Subtract(void* source, int elementOffset) where T : allows ref struct;
public static void* Subtract(void* source, int elementOffset);
[System.CLSCompliant(false)]
public static void* Subtract(void* source, int elementOffset);
[]
static member Subtract : nativeptr * int -> nativeptr
static member Subtract : nativeptr * int -> nativeptr

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(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(ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of Int32.

Applies to

Subtract(T, Int32)

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

Subtracts an offset from the given managed pointer.

public:
generic 
 static T % Subtract(T % source, int elementOffset);
public static ref T Subtract(ref T source, int elementOffset) where T : allows ref struct;
public static ref T Subtract(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(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(ref ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of Int32.

Applies to

Subtract(T, IntPtr)

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

Subtracts an element offset from the given managed pointer.

public:
generic 
 static T % Subtract(T % source, IntPtr elementOffset);
public static ref T Subtract(ref T source, IntPtr elementOffset) where T : allows ref struct;
public static ref T Subtract(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(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(ref ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of IntPtr.

Applies to

Subtract(T, UIntPtr)

Source:
Unsafe.cs
Source:
Unsafe.cs
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 
 static T % Subtract(T % source, UIntPtr elementOffset);
[System.CLSCompliant(false)]
public static ref T Subtract(ref T source, UIntPtr elementOffset) where T : allows ref struct;
public static ref T Subtract(ref T source, nuint elementOffset);
[System.CLSCompliant(false)]
public static ref T Subtract(ref T source, UIntPtr elementOffset);
[]
static member Subtract : 'T * unativeint -> 'T
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(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(ref ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of UIntPtr.

Applies to