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());
}