1

are there any differences in the references that are produced in code generated for anonymous methods by a .NET 2.0 or 4.0 compiler and code generated for an equivalent lambda by a .NET 4.0 compiler? and in particular for the this pointer: I know both anonymous methods and lambdas are a C# compiler feature and the compiler actually generates a nested class with a delegate and all the references required for outer variables, but this article on the implementation of anonymous methods states a reference is kept to the pointer and I cannot find any source describing anything similar for lambdas.. or am I not finding anything because the implementation for compiling anonymous methods maps 1 on 1 to that of lambdas?

here's a bit of code to demonstrate anonymous methods and lambdas:

    class AnonymousMethodMethodScope
    {
        private Func<bool> d;
        public Func<int, bool> d2;
        int j = 0;

        public void Test(int i)
        {
            d = new Func<bool>(delegate { j = 10; return j > i; });

            // what references does this anonymous method keep?
            d2 = new Func<int, bool>(delegate(int x) { return x == j; });

            Console.WriteLine("j = " + j + " result = " + d());
        }
    }

    class LambdaMethodScope
    {
        private Func<bool> d;
        public Func<int, bool> d2;

        public void Test(int i)
        {
            int j = 0;

            d = () => { j = 10; return j > i; };

            // what references does this lambda keep?
            d2 = x => x == j;

            Console.WriteLine("j = " + j + " result = " + d());
        }
    }
mtijn
  • 3,610
  • 2
  • 33
  • 55

1 Answers1

2

Yes, lambda expressions will do (and have to do) the same thing as anonymous methods when it comes to capturing variables. (I'm assuming you're talking about lambda expressions which are converted into delegates; if they're converted into expression trees they may be a bit different - I'm not sure.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I'm sorry to bring this up again but.. why?? why is the pointer also captured for the 'j' variable in the example above that has method scope? and what if the 'j' variable was static so it had type scope instead of class scope?? I haven't upvoted this answer yet because I'm still stuck with these questions. – mtijn Feb 03 '12 at 08:07
  • 1
    @mtijn: In your `LambdaMethodScope` example (which wasn't present when I wrote this answer) it *doesn't* need to capture `this`, because `j` is a local variable. In your `AnonymousMethodMethodScope` example it's an *instance* variable, so the anonymous method has to capture `this` in order to set `this.j` later. If you're going to make a comparison between two ways of doing something, you need to make sure you're doing the same thing in both examples! If `j` were static, it wouldn't need to capture anything. – Jon Skeet Feb 03 '12 at 08:26
  • 1
    @mtijn: Note that an anonymous function which *only* needs to capture `this` can just use an instance method created within the same type. That would work for your second anonymous method example - but not for the first, which *also* needs to capture a local variable (the parameter). – Jon Skeet Feb 03 '12 at 08:35