Enums in lambda expressions are compiled differently; consequence of overload resolution improvements?

删除回忆录丶 提交于 2019-12-31 08:31:54

问题


While trying out the Visual Studio 2015 RC, I received a run-time error on previously working code. Given the lambda (x => x.CustomerStatusID == CustomerStatuses.Active) which was passed to a function as an Expression<>, the debugger shows a difference in the expression tree. Formerly it compiled as this:

.Lambda #Lambda1<System.Func`2[Services.DataClasses.CustomerDC,System.Boolean]>(Services.DataClasses.CustomerDC $x)
{
    (System.Int32)$x.CustomerStatusID == 0
}

But in C# 6.0 it now compiles as

.Lambda #Lambda1<System.Func`2[Services.DataClasses.CustomerDC,System.Boolean]>(Services.DataClasses.CustomerDC $x)
{
    (System.Int32)$x.CustomerStatusID == (System.Int32).Constant<Services.DataClasses.CustomerStatuses>(Active)
}

Although the fix to my tree-traversing code was straightforward and the additional detail is appreciated, does anyone know of any other gotchas floating around like this?

Alternately, does anyone have a link to information on the specifics of how overload resolution was improved? I cannot find any.


回答1:


This has nothing to do with overload resolution. The previous compiler prematurely optimized the right-hand-side of the comparison, thereby omitting the code corresponding to the source expression.



来源:https://stackoverflow.com/questions/30538335/enums-in-lambda-expressions-are-compiled-differently-consequence-of-overload-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!