Roslyn: Breaking change and spec violation for code using underscore to refer to single lambda parameter

Created on 18 Jul 2020  ·  3Comments  ·  Source: dotnet/roslyn

Version Used: 16.6.4, SharpLab shows it on master

There's a lot of code out there like this:

using System.Linq;

class C
{
    void M()
    {
        // ❌ CS0103 The name '_' does not exist in the current context
        //                                ↓
        Enumerable.Range(0, 1).Select(_ = _.ToString());
    }
}

This doesn't follow the spec in https://github.com/dotnet/csharplang/blob/master/proposals/lambda-discard-parameters.md#detailed-design:

Note: if a single parameter is named _ then it is a regular parameter for backwards compatibility reasons.

Area-Compilers

Most helpful comment

Oh my, I'm so sorry! Wow 😆

All 3 comments

CC: @jcouv This should be covered by the test plan #38820

  • [x] that a single _ is not a discard parameter (ie. is in scope and can be read from) (see DiscardParameters_NotADiscardWhenSingleUnderscore)

I'm confused. There is no lambda in the example. Did you mean .Select(_ => _.ToString()) with a =>?

Oh my, I'm so sorry! Wow 😆

Was this page helpful?
0 / 5 - 0 ratings