Lets have a simple class
class User
{
public byte IdByte { get; set; }
}
So when I write expressions like these:
Expression<Func<User, bool>> expression1 = x => x.IdByte == 3;
Expression<Func<User, bool>> expression2 = x => x.IdByte == (byte)3;
byte b = 3;
Expression<Func<User, bool>> expression3 = x => x.IdByte == b;
Expression<Func<User, bool>> expression4 = x => x.IdByte == byte.MaxValue;
And watch that expressions debug view, I see that there is an additional type conversion to type System.Int32 :
//expression1 ------> x => (Convert(x.IdByte, Int32) == 3)
//expression2 ------> x => (Convert(x.IdByte, Int32) == 3)
//expression3 ------> x => (Convert(x.IdByte, Int32) == Convert(value(....c__DisplayClass1_0).b, Int32))
//expression4 ------> x => (Convert(x.IdByte, Int32) == 255)
In first and second expressions converting right side - 3 to byte is more reasonable than converting left side to int.
In rest of the cases left and right side is byte
My question is why these conversions applied ?