为什么使用/langversion:latest编译时未报告C#CS0136错误 [英] Why is C# CS0136 error not reported when compiled with /langversion:latest

查看:491
本文介绍了为什么使用/langversion:latest编译时未报告C#CS0136错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在Debug中可以正常编译,但由于在Release中出现CS0136(无法对本地或名为'x'的参数进行贴标..)错误而失败.有人知道为什么在调试版本中未报告该错误吗?

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;
}

目标框架4.6.1
VS版本:16.4.3和16.4.5
MSBuildVersion:16.4.0
MSBuildRuntimeVersion = 4.0.30319

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"

推荐答案

我能够找到部分答案: 此问题询问了同一件事. @Hans Passant的评论引用了此文件表示放宽了lambda的作用域规则.

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.

这可以解释为什么/langversion:latest和/langversion:7.3行为不同

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

但是对于.net 4.6.1项目,我假设/langversion:latest最多应导致C#7.3.但是显然不是这样,或者这里可能还有其他事情在发生.希望有人能对此有所启发.

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.

这篇关于为什么使用/langversion:latest编译时未报告C#CS0136错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆