Visual Studio and .NET Framework Home
Passing a lambda function to a base class in the constructor causes NullReferenceException when the function is called
as Won't Fix
6/29/2009 7:31:08 AM
User(s) can reproduce this bug
I have a base class that takes some delegate in its constructor. If I pass in a lambda function in the constructor of some derived class which references a method of the class, when the delegate is called later, a NullReferenceException is thrown.
I suspect that the implicit 'this' field is not being captured correctly in the closure.
Visual Studio 2008 SP1
Windows XP Professional
Operating System Language
Steps to Reproduce
Compile and run attached test case.
NullReferenceException is thrown.
Either the lambda function executes as expected, or a compile error.
TAP Code (if applicable)
You can indicate your satisfaction with how Microsoft handled this issue by completing this quick
3 question survey
to post a comment.
Please enter a comment.
on 3/15/2010 at 6:05 PM
Although this bug was closed as "won't fix", it seems to be fixed eventually : the C# 4 compiler gives an error when you try to do that. So you could change the status of this bug to "fixed" ;)
on 7/8/2009 at 12:13 PM
Thanks for the bug report for Visual Studio 2008!
Using this mechanism, you can indeed sneak the this pointer into a base call, which is something we otherwise try pretty hard to prevent! This is definitely code that should be a compiler error, but unfortunately, we're starting to lock down on what we can fix for the next release and improving the experience for error situations falls below the bar right now.
We'll definitely keep it in mind to clean up base calls in lambdas in our next release!
Visual C# Compiler
on 6/29/2009 at 9:41 PM
Thanks for your feedback.
We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
to post a workaround.
Please enter a workaround.
© 2014 Microsoft