Closed steve-warren closed 2 weeks ago
BinaryPrimitives
uses MemoryMarshal
under the hood:
/// <summary>
/// Reads a UInt16 out of a read-only span of bytes as little endian.
/// </summary>
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ReadUInt16LittleEndian(ReadOnlySpan<byte> source)
{
ushort result = MemoryMarshal.Read<ushort>(source);
if (!BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
}
It checks for endianness on every call. We can avoid this by implementing our own by:
Build a
BinaryPrimitives
wrapper class to reduce boilerplate code.e.g.