The problem is that due to castings and explicit operators:
(byte)objectExpression is not the same as (byte)sbyteExpression .
The first is [direct], which is not executed, because the real type of object is sbyte , not byte . The latter will perform a conversion that simply uses an explicit operator ("Explicit conversion") with syntax, which, unfortunately, still looks like [direct], as indicated above. The following is an example of a failed sans-database:
var obj = (object)(sbyte)0; var i1 = (int)(sbyte)obj; // okay: object (cast)-> sbyte (conversion)-> int var i2 = (int)obj; // fail: sbyte (cast)-> int (but sbyte is not int!)
Use either the (sbyte)objectExpression cast, which is valid for the real type of the object, or Convert.ToInt32(objectExpression) , which takes the object and does some magic to convert it to int. (Using Convert.ToByte can throw an overflow exception.)
Happy coding!
user166390
source share