Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do lambda expressions work internally?

While looking up the answer to this question: "Why is an out parameter not allowed within an anonymous method?" I've got a little lost about how do lambda expression and anonymous methods actually work.

In the comments JaredPar states that "Imagine for instance that the out parameter referred to a local variable on the stack. The lambda can execute at any arbitrary point in the future and hence could execute when that stack frame was no longer valid".

I pointed out if wouldn't that be the case with any other variable... which basically make me wonder what to I really know about lambda expressions.

The thing I have in mind is something like this:

public void Foo(ComplexObject val, out SomeDelegate outDelegate)
{
  ComplexObject obj = new ComplexObject(val)
  SomeDelegate = delegate(int other) { return (obj.value * other); }  
}

public void Bar()
{
  SomeDelegate MyDel = null;
  Foo(5, out MyDel);
  int finalRes = MyDel(100);
  // Whatever
}

In that situation I don't really know what's happening. obj is a reference on the stack which would no longer be valid on method return so the annonymous method should be able (if that works) to actually know that's a reference type and copy the reference instead of the value, if it does... why wouldn't ref params work if the "use case" is more or less the same?

like image 934
Jorge Córdoba Avatar asked Oct 28 '09 16:10

Jorge Córdoba


People also ask

How do lambda functions work?

You organize your code into Lambda functions. Lambda runs your function only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time that you consume—there is no charge when your code is not running.

How does lambda work in Scheme?

Lambda is the name of a special form that generates procedures. It takes some information about the function you want to create as arguments and it returns the procedure. It'll be easier to explain the details after you see an example.

Is lambda expression only body function?

The body of a lambda expression can contain zero, one or more statements. When there is a single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.

Can we use lambda without functional interface?

You do not have to create a functional interface in order to create lambda function.


2 Answers

I did a fairly extensive blog series on how closures work internally. It's written for the VB.Net implementation of closures but the underlying details are very similar to C#'s. It should provide the answers you're looking for

Here is the link to part 6 which links to all of the other articles

  • http://blogs.msdn.com/jaredpar/archive/2007/08/06/closures-in-vb-part-6-limitations.aspx
like image 169
JaredPar Avatar answered Nov 09 '22 03:11

JaredPar


Jon Skeet wrote an in-depth description.

Basically, the compiler turns the outer method into a class, and turns all of the variables that are accessed by the anonymous methods into fields on the class. The anonymous methods become regular instance methods on the class.

like image 26
SLaks Avatar answered Nov 09 '22 04:11

SLaks