Lambda parameter name (sometimes) conflicting with local name

前端 未结 1 578
我在风中等你
我在风中等你 2020-12-11 21:25

There are two projects, one is a library targeting .NET Standard 2.0, while the other is a console app targeting .NET Core 2.2.

Now this piece of code is working on

相关标签:
1条回答
  • 2020-12-11 21:55

    What is going on here?

    Basically, the ability to use a lambda expression parameter with the same name as an existing local variable is a new feature introduced in the C# 8 compiler - but there's been very little fuss about it. I happened to hear about it (from Mads himself) in a meeting this week, but otherwise wouldn't have known about it.

    The target framework matters because it changes the default language version that the compiler applies. You can specify it explicitly using the <LangVersion> element though. For example, with this project file:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net471</TargetFramework>
        <LangVersion>8.0</LangVersion>
      </PropertyGroup>
    </Project>
    

    ... the code builds fine. Ditto when targeting netstandard2.0 or netcoreapp2.2.

    0 讨论(0)
提交回复
热议问题