That's complicated in C#, because casting doubles to "byte friendly" values in C# isn't possible in "normal" code.
If you have a look at the Reference Sources however, you can see how they do it - and it's pretty simple:
[System.Security.SecuritySafeCritical]
public unsafe static byte[] GetBytes(double value)
{
Contract.Ensures(Contract.Result<byte[]>() != null);
Contract.Ensures(Contract.Result<byte[]>().Length == 8);
return GetBytes(*(long*)&value);
}
[System.Security.SecuritySafeCritical]
public unsafe static byte[] GetBytes(long value)
{
Contract.Ensures(Contract.Result<byte[]>() != null);
Contract.Ensures(Contract.Result<byte[]>().Length == 8);
byte[] bytes = new byte[8];
fixed(byte* b = bytes)
*((long*)b) = value;
return bytes;
}
That's the most efficient way to do it (ignore the Contract bits)
But
unsafe
code may not be what your homework asks for either...