Given two shorts (System.Int16)
short left = short.MaxValue; short right = 1;
I want to get an OverflowException when adding them.
checked(left+right)
does not work, because the result of left+right is an Int32.
checked((short)(left+right))
works as expected.
My problem is that, using Expression Trees, the ‘trick’ doesn’t work:
var a = Expression.Constant(left); var b = Expression.Constant(right); var sum = Expression.ConvertChecked(Expression.Add(a, b), typeof(short)); var l = Expression.Lambda(sum); var f = (Func<short>)l.Compile();
Calling f() does not throw an overflow exception but returns -32768. What’s wrong?
The problem is that the addition is being done as short + short (which presumably exists in IL even if it doesn’t exist in C#) – and then the conversion is performed separately. This is shown by this complete program – even without the conversion, the result is -32768:
If you make it do an int + int addition and then the conversion, it will throw an overflow exception:
I don’t know why
AddCheckeddoesn’t work though… that looks like a bug 🙁 It’s possible that using the overload which allows the Method to specified would work, but I’m not sure…