Unsafe.AddByteOffset Method

Definition

Overloads

AddByteOffset<T>(T, IntPtr)

Adds a byte offset to the given managed pointer.

AddByteOffset<T>(T, UIntPtr)

Adds a byte offset to the given managed pointer.

AddByteOffset<T>(T, IntPtr)

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

Adds a byte offset to the given managed pointer.

C#
public static ref T AddByteOffset<T>(ref T source, IntPtr byteOffset);

Type Parameters

T

The elemental type of the managed pointer.

Parameters

source
T

The managed pointer to add the offset to.

byteOffset
IntPtr

The offset to add.

Returns

T

A new managed pointer that reflects the addition of the specified byte offset to the source pointer.

Remarks

The byteOffset parameter is the number of bytes to add to the source pointer. For example, given a source pointer ptr of type ref int, the call Unsafe.AddByteOffset<int>(ref ptr, 20) will return a new pointer whose address points 20 bytes beyond ptr.

If byteOffset is a calculated value rather than a hardcoded literal, callers should consider the possibility of integer overflow. For example, in the call Unsafe.AddByteOffset<int>(ref ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of IntPtr.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.0 (package-provided)

AddByteOffset<T>(T, UIntPtr)

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

Important

This API is not CLS-compliant.

Adds a byte offset to the given managed pointer.

C#
[System.CLSCompliant(false)]
public static ref T AddByteOffset<T>(ref T source, UIntPtr byteOffset);
C#
public static ref T AddByteOffset<T>(ref T source, nuint byteOffset);

Type Parameters

T

The elemental type of the managed pointer.

Parameters

source
T

The managed pointer to add the offset to.

byteOffset

nuint

The offset to add.

Returns

T

A new managed pointer that reflects the addition of the specified byte offset to the source pointer.

Attributes

Remarks

The byteOffset parameter is the number of bytes to add to the source pointer. For example, given a source pointer ptr of type ref int, the call Unsafe.AddByteOffset<int>(ref ptr, (nuint)20) will return a new pointer whose address points 20 bytes beyond ptr.

If byteOffset is a calculated value rather than a hardcoded literal, callers should consider the possibility of integer overflow. For example, in the call Unsafe.AddByteOffset<int>(ref ptr, a * b), the caller must ensure the intermediate value a * b does not overflow the bounds of UIntPtr.

Applies to

.NET 10 and other versions
Product Versions
.NET 6, 7, 8, 9, 10
.NET Standard 2.0 (package-provided)