MemoryMarshal.TryRead<T>(ReadOnlySpan<Byte>, T) Method

Definition

Tries to read a structure of type T from a read-only span of bytes.

public:
generic <typename T>
 where T : value class static bool TryRead(ReadOnlySpan<System::Byte> source, [Runtime::InteropServices::Out] T % value);
public static bool TryRead<T> (ReadOnlySpan<byte> source, out T value) where T : struct;
static member TryRead : ReadOnlySpan<byte> * 'T -> bool (requires 'T : struct)
Public Shared Function TryRead(Of T As Structure) (source As ReadOnlySpan(Of Byte), ByRef value As T) As Boolean

Type Parameters

T

The type of the structure to retrieve.

Parameters

source
ReadOnlySpan<Byte>

A read-only span of bytes.

value
T

When the method returns, an instance of T.

Returns

true if the method succeeds in retrieving an instance of the structure; otherwise, false.

Exceptions

T contains managed object references.

Remarks

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

Caution

This method initializes an instance of T, including private instance fields and other implementation details, from the raw binary contents of the source span. Callers must ensure that the contents of the source span are well-formed with regard to T's internal invariants.

Applies to