How do lambdas scope to local variables?

Anthony Russell

So my understanding of how the compiler handles lambdas is limited.

My understanding is that the compiler takes your lambda and turns it into a real method.

If that's the case then how does it scope to local variables?

    public async Task<dynamic> GetWebStuff()
    {
        dynamic ret = "";

        WebClient wc = new WebClient();          

        wc.DownloadStringCompleted += async (s, a) => 
        {
            ret = await Newtonsoft.Json.JsonConvert.DeserializeObject(a.Result.ToString());
        };

        wc.DownloadString("http://www.MyJson.com");

        return ret;
    }

The above example will set the return value of ret to the caller which is a dynamic object of deserialized JSON.

How does that happen though if the compiler takes that completed event lambda and abstracts it into its own method? How does it know to set the ret value?

It's like me saying this (which obviously wont work)

        public async Task<dynamic> GetWebStuff()
        {
            dynamic ret = "";

            WebClient wc = new WebClient();

            wc.DownloadStringCompleted += wc_DownloadStringCompleted;            

            wc.DownloadString("google.com");

            return ret;
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            ret = await Newtonsoft.Json.JsonConvert.DeserializeObject(e.Result.ToString());
        }
Selman Genç

It does that creating an anonymous class. For example consider this code:

int x = 0;

Action action = () => x = 2;

action();

Console.Write(x);

And the generated class :

enter image description here

IL code of the <Main>b__2 method which sets the value of x:

    .method assembly hidebysig instance void 
        '<Main>b__2'() cil managed
{
  // Code size       10 (0xa)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.2
  IL_0002:  stfld      int32 ConsoleApplication1.Program/'<>c__DisplayClass0'::x
  IL_0007:  br.s       IL_0009
  IL_0009:  ret
} // end of method '<>c__DisplayClass0'::'<Main>b__2'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do asynchronous lambdas in Java scope to local variables

From Dev

How do asynchronous lambdas in Java scope to local variables

From Dev

Lambdas and capture by reference local variables : Accessing after the scope

From Dev

How many variables can be in local scope

From Dev

How many variables can be in local scope

From Dev

Scope of local variables in Python

From Dev

Local variables in Lambdas vs Anonymous inner classes

From Dev

How do I keep awk variables in scope?

From Dev

With a Javascript Promise, how to close over variables local to the parent scope in a `then`?

From Dev

How local variables are handled when referenced in another scope?

From Dev

Prolog: how does variable instantiating work, what is the scope of local variables

From Dev

Prolog: how does variable instantiating work, what is the scope of local variables

From Dev

With a Javascript Promise, how to close over variables local to the parent scope in a `then`?

From Dev

Scope and lifetime of local variables in C

From Dev

R: scope of local/global variables

From Dev

Scope and lifetime of local variables in C

From Dev

Do local inner classes maintain a copy of all local variables in the scope that they are defined in?

From Java

Lambdas: local variables need final, instance variables don't

From Dev

How do local variables get stored in stack?

From Dev

How do local variables work with Python closures?

From Dev

How do I access outer scope variables from a callback in Angular?

From Dev

How do i pass my scope correctly to look for variables

From Dev

PHP: How do I free limited scope variables?

From Dev

How do I resolve the scope issue with my variables and/or functions?

From Dev

How do I capture variables outside the scope of a closure in Rust?

From Dev

Why do local variables that reference a global object seem to violate function scope?

From Dev

How the variables are out of scope?

From Dev

Struggle with EJS and scope of -not so- "local" variables

From Java

Local variables definition in relation to scope, C++

Related Related

  1. 1

    How do asynchronous lambdas in Java scope to local variables

  2. 2

    How do asynchronous lambdas in Java scope to local variables

  3. 3

    Lambdas and capture by reference local variables : Accessing after the scope

  4. 4

    How many variables can be in local scope

  5. 5

    How many variables can be in local scope

  6. 6

    Scope of local variables in Python

  7. 7

    Local variables in Lambdas vs Anonymous inner classes

  8. 8

    How do I keep awk variables in scope?

  9. 9

    With a Javascript Promise, how to close over variables local to the parent scope in a `then`?

  10. 10

    How local variables are handled when referenced in another scope?

  11. 11

    Prolog: how does variable instantiating work, what is the scope of local variables

  12. 12

    Prolog: how does variable instantiating work, what is the scope of local variables

  13. 13

    With a Javascript Promise, how to close over variables local to the parent scope in a `then`?

  14. 14

    Scope and lifetime of local variables in C

  15. 15

    R: scope of local/global variables

  16. 16

    Scope and lifetime of local variables in C

  17. 17

    Do local inner classes maintain a copy of all local variables in the scope that they are defined in?

  18. 18

    Lambdas: local variables need final, instance variables don't

  19. 19

    How do local variables get stored in stack?

  20. 20

    How do local variables work with Python closures?

  21. 21

    How do I access outer scope variables from a callback in Angular?

  22. 22

    How do i pass my scope correctly to look for variables

  23. 23

    PHP: How do I free limited scope variables?

  24. 24

    How do I resolve the scope issue with my variables and/or functions?

  25. 25

    How do I capture variables outside the scope of a closure in Rust?

  26. 26

    Why do local variables that reference a global object seem to violate function scope?

  27. 27

    How the variables are out of scope?

  28. 28

    Struggle with EJS and scope of -not so- "local" variables

  29. 29

    Local variables definition in relation to scope, C++

HotTag

Archive