1

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"
Xiaoguo Ge
  • 2,177
  • 20
  • 26

1 Answers1

0

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.

Xiaoguo Ge
  • 2,177
  • 20
  • 26
  • 1
    "For a .net 4.6.1 project though, I would assume /langversion:latest should result in at most C# 7.3." - nope, it can still handle *most* C# 8 features. – Jon Skeet Mar 09 '20 at 07:12
  • @JonSkeet Looks like I misunderstood default language version provided on [this page](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version). Apparently /langversion:latest does not mean default. It overwrites default and uses the C# 8.0 when it is available. – Xiaoguo Ge Mar 09 '20 at 07:25
  • Yes, exactly - default is only what it says it is: the default if you don't specify anything else. – Jon Skeet Mar 09 '20 at 07:29