Skip to content

Avoid Unsafe.As in BitConverter #112616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 7, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/libraries/System.Private.CoreLib/src/System/BitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static bool TryWriteBytes(Span<byte> destination, bool value)
public static byte[] GetBytes(char value)
{
byte[] bytes = new byte[sizeof(char)];
Unsafe.As<byte, char>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand All @@ -81,7 +81,7 @@ public static bool TryWriteBytes(Span<byte> destination, char value)
public static byte[] GetBytes(short value)
{
byte[] bytes = new byte[sizeof(short)];
Unsafe.As<byte, short>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand All @@ -108,7 +108,7 @@ public static bool TryWriteBytes(Span<byte> destination, short value)
public static byte[] GetBytes(int value)
{
byte[] bytes = new byte[sizeof(int)];
Unsafe.As<byte, int>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand All @@ -135,7 +135,7 @@ public static bool TryWriteBytes(Span<byte> destination, int value)
public static byte[] GetBytes(long value)
{
byte[] bytes = new byte[sizeof(long)];
Unsafe.As<byte, long>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand All @@ -162,7 +162,7 @@ public static bool TryWriteBytes(Span<byte> destination, long value)
public static byte[] GetBytes(Int128 value)
{
byte[] bytes = new byte[Int128.Size];
Unsafe.As<byte, Int128>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand Down Expand Up @@ -190,7 +190,7 @@ public static bool TryWriteBytes(Span<byte> destination, Int128 value)
public static byte[] GetBytes(ushort value)
{
byte[] bytes = new byte[sizeof(ushort)];
Unsafe.As<byte, ushort>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand Down Expand Up @@ -219,7 +219,7 @@ public static bool TryWriteBytes(Span<byte> destination, ushort value)
public static byte[] GetBytes(uint value)
{
byte[] bytes = new byte[sizeof(uint)];
Unsafe.As<byte, uint>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand Down Expand Up @@ -248,7 +248,7 @@ public static bool TryWriteBytes(Span<byte> destination, uint value)
public static byte[] GetBytes(ulong value)
{
byte[] bytes = new byte[sizeof(ulong)];
Unsafe.As<byte, ulong>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand Down Expand Up @@ -277,7 +277,7 @@ public static bool TryWriteBytes(Span<byte> destination, ulong value)
public static byte[] GetBytes(UInt128 value)
{
byte[] bytes = new byte[UInt128.Size];
Unsafe.As<byte, UInt128>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand Down Expand Up @@ -305,7 +305,7 @@ public static bool TryWriteBytes(Span<byte> destination, UInt128 value)
public static unsafe byte[] GetBytes(Half value)
{
byte[] bytes = new byte[sizeof(Half)];
Unsafe.As<byte, Half>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand All @@ -332,7 +332,7 @@ public static unsafe bool TryWriteBytes(Span<byte> destination, Half value)
public static byte[] GetBytes(float value)
{
byte[] bytes = new byte[sizeof(float)];
Unsafe.As<byte, float>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand All @@ -359,7 +359,7 @@ public static bool TryWriteBytes(Span<byte> destination, float value)
public static byte[] GetBytes(double value)
{
byte[] bytes = new byte[sizeof(double)];
Unsafe.As<byte, double>(ref bytes[0]) = value;
MemoryMarshal.Write(bytes, value);
return bytes;
}

Expand Down
Loading