11

Is there a difference in the declaration of lambda expressions between the .NET Framework and .NET Core?

The following expressions compiles in .NET Core:

var lastShift = timeline.Appointments
                        .OfType<DriverShiftAppointment>()
                        .SelectMany(x => x.Activities.Where(x => !x.IsTheoretical))
                        .OrderByDescending(x => x.Start)
                        .FirstOrDefault();

But not in the .NET framework.

The problem in this expression is the following part

.SelectMany(x => x.Activities.Where(x => !x.IsTheoretical))

where x is declared twice (SelectMany and Where).

This is the error in the .NET Framework:

A local or parameter named 'x' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

.NET framework

enter image description here

Reproducable example:

public class DemoClass
{
    public IList<int> Numbers { get; set; } = new List<int>();
}

class Program
{
    static void Main(string[] args)
    {
        var lst = new List<DemoClass>
        {
            new DemoClass
            {
                Numbers = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8 }
            },
            new DemoClass
            {
                Numbers = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8 }
            },
            new DemoClass
            {
                Numbers = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8 }
            }
        };

        var result = lst.SelectMany(x => x.Numbers.Where(x => x % 2 == 0))
                        .OrderByDescending(x => x);

        Console.WriteLine("Hello World!");
    }
}
7
  • @500-InternalServerError sorry, missed that part. Commented Mar 24, 2021 at 9:35
  • Interesting... Can you create a minimal reproducible example we can copy/paste? Commented Mar 24, 2021 at 9:37
  • @canton7 I'll make one Commented Mar 24, 2021 at 9:39
  • 1
    @canton7 I've added a very basic and complete sample. Commented Mar 24, 2021 at 9:47
  • 2
    Interestingly, SharpLab is fine, but dotnetfiddle isn't Commented Mar 24, 2021 at 10:01

1 Answer 1

11

It is telling you that problem is this line:

.SelectMany(x => x.Activities.Where(x => !x.IsTheoretical))

Using the x twice confuses it, it is ambiguous, try this:

.SelectMany(x => x.Activities.Where(y => !y.IsTheoretical))

But you are right, it compiles in core but not framework. It looks to be like this: https://github.com/dotnet/roslyn/issues/38377. Reading that link, it looks like this is a change in C# 8.0, and core is targeting 8.0 while framework is targeting 7.2.

Sign up to request clarification or add additional context in comments.

5 Comments

That is totally clear and easy to solve, but not the problem. The problem is, why is it working under .net core?
Probably differences in C# compiler. Anyway try to avoid this.
@SvyatoslavDanyliv yes, it is already fixed, just seen this when using the same code in .net core and .net framework.
@BendEg yes you are correct, I missed that point. I don't know why this is.
Nice find on the issue. It would be good if you could update your answer to say that this is a change introduced in C# 8, and not a .NET Core / .NET Framework difference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.