MemoryMarshal.TryWrite<T>(Span<Byte>, T) Method

Definition

Tries to write a structure of type T into a span of bytes.

public:
generic <typename T>
 where T : value class static bool TryWrite(Span<System::Byte> destination, T % value);
public static bool TryWrite<T> (Span<byte> destination, ref T value) where T : struct;
public static bool TryWrite<T> (Span<byte> destination, in T value) where T : struct;
static member TryWrite : Span<byte> * 'T -> bool (requires 'T : struct)
Public Shared Function TryWrite(Of T As Structure) (destination As Span(Of Byte), ByRef value As T) As Boolean

Type Parameters

T

The type of the structure.

Parameters

destination
Span<Byte>

The span of bytes to contain the structure.

value
T

The structure to be written to the span.

Returns

true if the write operation succeeded; otherwise, false. The method returns false if the span is too small to contain T.

Exceptions

T contains managed object references.

Remarks

T cannot contain managed object references. The TryWrite method performs this check at runtime and throws ArgumentException if the check fails.

Caution

This method copies raw binary data from the original span, including any private instance fields and other implementation details of type T. Callers should ensure that their code is resilient to changes in the internal layout of T.

Applies to