diff --git a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
index 18b6ab4b0b5346..8b2ebcc6db1376 100644
--- a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
@@ -29,6 +29,7 @@ public static class BitConverter
///
/// A Boolean value.
/// A byte array with length 1.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(bool value)
{
byte[] r = new byte[1];
@@ -56,10 +57,11 @@ public static bool TryWriteBytes(Span destination, bool value)
///
/// A Char value.
/// An array of bytes with length 2.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(char value)
{
byte[] bytes = new byte[sizeof(char)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -83,10 +85,11 @@ public static bool TryWriteBytes(Span destination, char value)
///
/// The number to convert.
/// An array of bytes with length 2.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(short value)
{
byte[] bytes = new byte[sizeof(short)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -110,10 +113,11 @@ public static bool TryWriteBytes(Span destination, short value)
///
/// The number to convert.
/// An array of bytes with length 4.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(int value)
{
byte[] bytes = new byte[sizeof(int)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -137,10 +141,11 @@ public static bool TryWriteBytes(Span destination, int value)
///
/// The number to convert.
/// An array of bytes with length 8.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(long value)
{
byte[] bytes = new byte[sizeof(long)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -165,10 +170,11 @@ public static bool TryWriteBytes(Span destination, long value)
/// The number to convert.
/// An array of bytes with length 2.
[CLSCompliant(false)]
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(ushort value)
{
byte[] bytes = new byte[sizeof(ushort)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -194,10 +200,11 @@ public static bool TryWriteBytes(Span destination, ushort value)
/// The number to convert.
/// An array of bytes with length 4.
[CLSCompliant(false)]
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(uint value)
{
byte[] bytes = new byte[sizeof(uint)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -223,10 +230,11 @@ public static bool TryWriteBytes(Span destination, uint value)
/// The number to convert.
/// An array of bytes with length 8.
[CLSCompliant(false)]
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(ulong value)
{
byte[] bytes = new byte[sizeof(ulong)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -251,10 +259,11 @@ public static bool TryWriteBytes(Span destination, ulong value)
///
/// The number to convert.
/// An array of bytes with length 2.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe byte[] GetBytes(Half value)
{
byte[] bytes = new byte[sizeof(Half)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -278,10 +287,11 @@ public static unsafe bool TryWriteBytes(Span destination, Half value)
///
/// The number to convert.
/// An array of bytes with length 4.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(float value)
{
byte[] bytes = new byte[sizeof(float)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}
@@ -305,10 +315,11 @@ public static bool TryWriteBytes(Span destination, float value)
///
/// The number to convert.
/// An array of bytes with length 8.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytes(double value)
{
byte[] bytes = new byte[sizeof(double)];
- Unsafe.As(ref bytes[0]) = value;
+ Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(bytes), value);
return bytes;
}