Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that the addition <em>is</em> 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:</p> <pre><code>using System; using System.Linq.Expressions; class Test { static void Main(string[] args) { short left = short.MaxValue; short right = 1; var a = Expression.Constant(left); var b = Expression.Constant(right); var sum = Expression.Add(a, b); var convert = Expression.ConvertChecked(sum, typeof(short)); var convertLambda = Expression.Lambda&lt;Func&lt;short&gt;&gt;(convert); var convertFunc = convertLambda.Compile(); Console.WriteLine("Conversion: {0}", convertFunc()); var sumLambda = Expression.Lambda&lt;Func&lt;short&gt;&gt;(sum); var sumFunc = sumLambda.Compile(); Console.WriteLine("Sum: {0}", sumFunc()); } } </code></pre> <p>If you make it do an int + int addition and then the conversion, it will throw an overflow exception:</p> <pre><code>using System; using System.Linq.Expressions; class Test { static void Main(string[] args) { short left = short.MaxValue; short right = 1; var a = Expression.Constant((int) left); var b = Expression.Constant((int) right); var sum = Expression.Add(a, b); var convert = Expression.ConvertChecked(sum, typeof(short)); var convertLambda = Expression.Lambda&lt;Func&lt;short&gt;&gt;(convert); var convertFunc = convertLambda.Compile(); Console.WriteLine("Conversion: {0}", convertFunc()); var sumLambda = Expression.Lambda&lt;Func&lt;int&gt;&gt;(sum); var sumFunc = sumLambda.Compile(); Console.WriteLine("Sum: {0}", sumFunc()); } } </code></pre> <p>I don't know why <code>AddChecked</code> doesn't work though... that looks like a bug :( It's possible that using <a href="http://msdn.microsoft.com/en-us/library/bb336728.aspx" rel="nofollow noreferrer">the overload which allows the Method to specified would work</a>, but I'm not sure...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload