8

Usually, when a method contains a lambda expression somewhere in it, if you edit that method, Visual Studio will say:

Modifying a 'method' which contains a lambda expression will prevent the debug session from continuing while Edit and Continue is enabled.

Is there a way to avoid this error?

In my case, I have a class whose constructor accepts an Action.

Button(Texture2D t2d, Vector2 v2, Action onPress)
...
Button b = new Button(t2d, new Vector2(40, 60), () => { MainStatic.t = t; });

Additional information on this issue:

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • 1
    Not possible, see here: http://stackoverflow.com/a/582138/1909055 – Mitch Jan 06 '13 at 15:58
  • I don't understand how I can not avoid it with a constructor like mine. – user1306322 Jan 06 '13 at 15:59
  • 3
    Since I cannot answer because it's been marked as duplicate (not true IMHO): my workaround is to copy-paste the method, edit it, delete the original method. As stupid as this sounds, it works and it's a fast way to avoiding restarting the debug. – alelom Aug 27 '19 at 09:02

1 Answers1

14

UPDATE: The desired feature was added in Visual Studio 2015, after many requests from users for this feature. This answer, and the question, are now out of date.


Is there a way to avoid this error?

Yes. Remove the lambda from the method. Or, don't edit the method.

Is there a way to avoid this error without removing the lambda from the method and still editing the method?

No. The error message is not lying to you.

The reason for this, if you're curious, is because lambdas are compiled as methods of a nested class, and local variables that the lambda closes over become fields of that class. The edit-and-continue feature rewrites the current method on-the-fly as you edit it, but even simple edits can result in complex changes to those nested classes. Rather than spend an enormous amount of effort on making E&C work for this scenario, and thereby steal resources from other, more valuable features, the debugger team simply made it illegal.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • But is there a way to change lambda expression into something different and then be able to edit and whatnot? – user1306322 Jan 06 '13 at 18:16
  • 1
    @user1306322: Sure. Change it into something that is not a lambda expression. For example, change it into a method of a nested class. If you do the work of building the nested class and analyzing when it needs to be changed on an edit then the debugger does not have to do so for you. – Eric Lippert Jan 06 '13 at 18:37
  • Will Roslyn make this feature easier for the VS team to implement? (Given that its capable of incremental compilation, as evidenced in the C# interactive window) – MgSam Jan 08 '13 at 16:49
  • @MgSam: Regrettably, probably not right away. The plan of record when I left was to NOT rewrite the already-working and extremely complex expression analyzer into Roslyn, and even if they did, it wouldn't help much; the real work needs to be done to allow edit-and-continue to better handle generation of new classes and methods on the fly. – Eric Lippert Jan 08 '13 at 17:24
  • 3
    @EricLippert That's disappointing to hear. Enabling lambdas in edit and continue and the watch/continue windows is the 3rd highest voted issue on the Visual Studio Connect site. – MgSam Jan 08 '13 at 18:39
  • The Visual Studio team is taking a survey regarding this. Please share your feedback: http://aka.ms/encsurvey – Dan Puza May 19 '14 at 21:23
  • 2
    Reading the UPDATE part on this supposed new feature: **Visual Studio Community 2017 here, the error still appears**. Anybody knows if they rolled back the changes? – alelom Aug 27 '19 at 08:54
  • Yeah, this is disappointing - still running into it in 2021 using a simple ?? operator. It's great synctactic sugar from a readability standpoint, but the fact that it requires rerunning the app (atleast in Rider) is disappointing to me. Maybe it'll be different in VS22 – mochsner Sep 09 '21 at 18:29
  • 2
    Have still the same error in VS2022. – Beetee Apr 01 '22 at 08:47