Sorry, the blog post you were talking about is incorrect. Lambda expressions do not have a common problem with memory leaks. In the blog examples, finalizers are never called because the author never removes anonymous methods from the event. Therefore, the .NET runtime believes that the method can still be called later and cannot remove the class from memory.
In your code, you will select an instance of NewProjectUI somewhere, and this is the moment when all events become unsuccessful and when the assigned lambda method also becomes unused. GC can then remove the anonymous lambda helper class and free up used memory.
So, again: in .NET there is no problem using local variables in lambda expressions.
But to make the code better, move the code from lambda expressions to named methods and add these methods to events. It also allows you to remove methods from events when they are no longer needed.
source share