what happens is that only the last 8 bits are taken when you cast the number. Let me explain with an example using ints. I use int for simplicity, because if you cast double to a non-float number representation than the decimal places are discarded anyway.
First we look at the binary representation of the number 345:
string binaryInt = Convert.ToString(345, 2);
Console.WriteLine(binaryInt);
The output is :
1 0101 1001
When we now take only the last 8 bits, which is the size of a byte type:
string eightLastBits = binaryInt.Substring(binaryInt.Length-8);
Console.WriteLine(eightLastBits);
We get:
0101 1001
If we convert this to byte again you will see that the result is 89:
byte backAgain = Convert.ToByte(eightLastBits, 2);
Console.WriteLine(backAgain);
Output:
89
EDIT:
As pointed out by InBetween the int example is the real deal and what really happens during casting.
Looking at the implementation of the conversion of double to byte:
/// <internalonly/>
byte IConvertible.ToByte(IFormatProvider provider) {
return Convert.ToByte(m_value);
}
If we look at the Convert.ToByte method implementation: we see that the double if first converted to an integer:
public static byte ToByte(double value) {
return ToByte(ToInt32(value));
}