MemoryMarshal.Cast Method

Definition

Overloads

Cast<TFrom,TTo>(ReadOnlySpan<TFrom>)

Casts a read-only span of one primitive type to a read-only span of another primitive type.

Cast<TFrom,TTo>(Span<TFrom>)

Casts a span of one primitive type to a span of another primitive type.

Cast<TFrom,TTo>(ReadOnlySpan<TFrom>)

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

Casts a read-only span of one primitive type to a read-only span of another primitive type.

public:
generic <typename TFrom, typename TTo>
 where TFrom : value class where TTo : value class static ReadOnlySpan<TTo> Cast(ReadOnlySpan<TFrom> span);
public static ReadOnlySpan<TTo> Cast<TFrom,TTo> (ReadOnlySpan<TFrom> span) where TFrom : struct where TTo : struct;
static member Cast : ReadOnlySpan<'From (requires 'From : struct)> -> ReadOnlySpan<'o (requires 'o : struct)> (requires 'From : struct and 'o : struct)
Public Shared Function Cast(Of TFrom As Structure, TTo As Structure) (span As ReadOnlySpan(Of TFrom)) As ReadOnlySpan(Of TTo)

Type Parameters

TFrom

The type of the source span.

TTo

The type of the target span.

Parameters

span
ReadOnlySpan<TFrom>

The source slice to convert.

Returns

The converted read-only span.

Exceptions

TFrom or TTo contains managed object references.

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

Remarks

Neither TFrom nor TTo can contain managed object references. The Cast method performs this check at runtime and throws ArgumentException if the check fails.

This method is supported only on platforms that support misaligned memory access or when the memory block is aligned by other means.

Applies to

Cast<TFrom,TTo>(Span<TFrom>)

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

Casts a span of one primitive type to a span of another primitive type.

public:
generic <typename TFrom, typename TTo>
 where TFrom : value class where TTo : value class static Span<TTo> Cast(Span<TFrom> span);
public static Span<TTo> Cast<TFrom,TTo> (Span<TFrom> span) where TFrom : struct where TTo : struct;
static member Cast : Span<'From (requires 'From : struct)> -> Span<'o (requires 'o : struct)> (requires 'From : struct and 'o : struct)
Public Shared Function Cast(Of TFrom As Structure, TTo As Structure) (span As Span(Of TFrom)) As Span(Of TTo)

Type Parameters

TFrom

The type of the source span.

TTo

The type of the target span.

Parameters

span
Span<TFrom>

The source slice to convert.

Returns

Span<TTo>

The converted span.

Exceptions

TFrom or TTo contains managed object references.

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

Remarks

Neither TFrom nor TTo can contain managed object references. The Cast method performs this check at runtime and throws ArgumentException if the check fails.

If the sizes of the two types are different, the cast combines or splits values, which leads to a change in length.

For example, if TFrom is Int64, the ReadOnlySpan<Int64> contains a single value, 0x0100001111110F0F, and TTo is Int32, the resulting ReadOnlySpan<Int32> contains two values. The values are 0x11110F0F and 0x01000011 on a little-endian architecture, such as x86. On a big-endian architecture, the order of the two values is reversed, i.e. 0x01000011, followed by 0x11110F0F.

As another example, if TFrom is Int32, the ReadOnlySpan<Int32> contains the values of 1, 2 and 3, and TTo is Int64, the resulting ReadOnlySpan<Int64> contains a single value: 0x0000000200000001 on a little-endian architecture and 0x0000000100000002 on a big-endian architecture.

This method is supported only on platforms that support misaligned memory access or when the memory block is aligned by other means.

Applies to