MemoryMarshal.AsBytes Method

Definition

Overloads

AsBytes<T>(ReadOnlySpan<T>)

Casts a ReadOnlySpan<T> of one primitive type, T, to a ReadOnlySpan<Byte>.

AsBytes<T>(Span<T>)

Casts a Span<T> of one primitive type, T, to a Span<Byte>.

AsBytes<T>(ReadOnlySpan<T>)

Source:
MemoryMarshal.cs
Source:
MemoryMarshal.cs
Source:
MemoryMarshal.cs

Casts a ReadOnlySpan<T> of one primitive type, T, to a ReadOnlySpan<Byte>.

public:
generic <typename T>
 where T : value class static ReadOnlySpan<System::Byte> AsBytes(ReadOnlySpan<T> span);
public static ReadOnlySpan<byte> AsBytes<T> (ReadOnlySpan<T> span) where T : struct;
static member AsBytes : ReadOnlySpan<'T (requires 'T : struct)> -> ReadOnlySpan<byte> (requires 'T : struct)
Public Shared Function AsBytes(Of T As Structure) (span As ReadOnlySpan(Of T)) As ReadOnlySpan(Of Byte)

Type Parameters

T

The type of items in the read-only span.

Parameters

span
ReadOnlySpan<T>

The source slice to convert.

Returns

A read-only span of type Byte.

Exceptions

T contains managed object references.

The Length property of the new ReadOnlySpan<T> would exceed Int32.MaxValue

Remarks

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

Caution

This method provides a raw binary projection over the original span, including over 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

AsBytes<T>(Span<T>)

Source:
MemoryMarshal.cs
Source:
MemoryMarshal.cs
Source:
MemoryMarshal.cs

Casts a Span<T> of one primitive type, T, to a Span<Byte>.

public:
generic <typename T>
 where T : value class static Span<System::Byte> AsBytes(Span<T> span);
public static Span<byte> AsBytes<T> (Span<T> span) where T : struct;
static member AsBytes : Span<'T (requires 'T : struct)> -> Span<byte> (requires 'T : struct)
Public Shared Function AsBytes(Of T As Structure) (span As Span(Of T)) As Span(Of Byte)

Type Parameters

T

The type of items in the span.

Parameters

span
Span<T>

The source slice to convert.

Returns

A span of type Byte.

Exceptions

T contains managed object references.

The Length property of the new ReadOnlySpan<T> would exceed Int32.MaxValue

Remarks

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

Caution

This method provides a raw binary projection over the original span, including over 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