Why is C# CS0136 error not reported when compiled with /langversion:latest [duplicate]

拟墨画扇 提交于 2020-03-21 07:00:13

问题


The code below compiles fine in Debug but failed with a CS0136(A local or parameter named 'x' cannot be decaled..) error in Release. Anybody has any idea why the error is not reported in Debug build?

public void test()
{
    Action<int> a = x => x++;
    int x = 0;
}

Target framework 4.6.1
VS version: 16.4.3 and 16.4.5
MSBuildVersion: 16.4.0
MSBuildRuntimeVersion = 4.0.30319

After some investigation, I was able to trace the error to this configuration difference between debug and release build

Debug build has this line in config, but release build does not have.

    <LangVersion>latest</LangVersion>

and this problem can be reproduced with the following two command

csc.exe /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.dll" /langversion:7.3 Program.cs"
csc.exe /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.dll" /langversion:latest Program.cs"

回答1:


I was able to find some partial answers: This question asked about the same thing. A comment from @Hans Passant referenced This Document which indicates there is a relaxation of the scoping rule for lambdas.

This can explain why /langversion:latest and /langversion:7.3 behave differently

For a .net 4.6.1 project though, I would assume /langversion:latest should result in at most C# 7.3. But apparently it is not the case or maybe there is something else going on here. Hope somebody can shed more light on this.



来源:https://stackoverflow.com/questions/60388747/why-is-c-sharp-cs0136-error-not-reported-when-compiled-with-langversionlatest

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