lambda expressions in immediate window for VS2015
Asked Answered
P

1

36

I am running Visual Studio 2015, where people claim that lambda expressions are supported in immediate window, watch and other places. But that does not work for me. I created a new console application with the following code:

static void Main(string[] args)
{
    var list = new List<int> {1, 2, 3, 4, 5, 6, 7};
}

And then tried some simple lambdas in immediate window and watch as can be seen here: enter image description here

And as you can see, it gives me "Expression cannot contain lambda expressions". I've looked in tools -> options -> debug settings but could not find anything related to the issue. The version I am running is VS2015 Enterprise, 14.0.24720.00 Update 1 What could be causing this issue?

Phytophagous answered 11/4, 2016 at 21:16 Comment(1)
Make sure 'Managed Compatibility mode' and 'legacy expressions' are disabled in the debug options.Harijan
R
50

This definitely works for me, looks like when I tick either Use Managed Compatibility Mode or Use the legacy C# and VB expression evaluators it stops working. Ensure those are unchecked.

enter image description here

Roughneck answered 11/4, 2016 at 21:43 Comment(8)
Saved my life. Have being trying to figure this out for a long time. Somehow, google did not bring me here.Babblement
I have the same issue. I have both of them unchecked but I still get the same issue.error CS1061: 'List<XXX>' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'List<XXX>' could be found (are you missing a using directive or an assembly reference?)Revisal
Since you cannot run using statements in the Immediate Window, you will need to add the using System.Linq; statement to the class you are debugging. Once you do that, the Immediate Window should recognize the extension methods.Roughneck
This may be obvious to some, but you cannot change these options while running your code(they are greyed out), VS has to be stopped, then these options can be changed.Calumet
It was not working for me either in Visual Studio 2017. But in my case it was caused by a library referenced in my project via NuGet: Alea. In case it helps some people...Deplore
I need to Use Managed Compatibility Mode... Is there a way to still use lambda expressions?Dunlop
@Guillaume: Do you remember how you figured out that a NuGet package was causing problems?Rapids
@ProtectorOne no sorry... And I don't have access to this source code anymore so I can't even check it in case it would remind me... But I don't think it was so much about NuGet than the package itself. In my case the package - Alea - was playing with the IL. So it's possible the resulting compiled code was not what the VS debugger was expecting in order to execute the lambdas... Could have just been an educated guess based on that.Deplore

© 2022 - 2024 — McMap. All rights reserved.