Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the lifetime of a delegate created by a lambda in C#?

Lambdas are nice, as they offer brevity and locality and an extra form of encapsulation. Instead of having to write functions which are only used once you can use a lambda.

While wondering how they worked, I intuitively figured they are probably only created once. This inspired me to create a solution which allows to restrict the scope of a class member beyond private to one particular scope by using the lambda as an identifier of the scope it was created in.

This implementation works, although perhaps overkill (still researching it), proving my assumption to be correct.

A smaller example:

class SomeClass {     public void Bleh()     {         Action action = () => {};     }      public void CallBleh()     {         Bleh();  // `action` == {Method = {Void <SomeClass>b__0()}}         Bleh();  // `action` still == {Method = {Void <SomeClass>b__0()}}     } } 

Would the lambda ever return a new instance, or is it guaranteed to always be the same?

like image 810
Steven Jeuris Avatar asked Jun 08 '11 14:06

Steven Jeuris


People also ask

What is delegate lambda expression?

Lambda expression is an anonymous method that you can use to create delegates or expression tree types. Microsoft introduced Lambda Expression in C# 3.0 sometime in 2007. Most of the time, anonymous methods are confused with lambda expression by many developers.

Are lambdas delegates?

Again, lambdas are just delegates, which means that they can be used as an event handler without any problems, as the following code snippet illustrates. The += operator in this context is used to subscribe to an event.

What is lambda function in C++11?

In C++11 and later, a lambda expression—often called a lambda—is a convenient way of defining an anonymous function object (a closure) right at the location where it's invoked or passed as an argument to a function.

Which is a valid type for this lambda function?

A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid. Lambda expressions can be functions or subroutines and can be single-line or multi-line. You can pass values from the current scope to a lambda expression. The RemoveHandler statement is an exception.


1 Answers

It's not guaranteed either way.

From what I remember of the current MS implementation:

  • A lambda expression which doesn't capture any variables is cached statically
  • A lambda expression which only captures "this" could be captured on a per-instance basis, but isn't
  • A lambda expression which captures a local variable can't be cached
  • Two lambda expressions which have the exact same program text aren't aliased; in some cases they could be, but working out the situations in which they can be would be very complicated
  • EDIT: As Eric points out in the comments, you also need to consider type arguments being captured for generic methods.

EDIT: The relevant text of the C# 4 spec is in section 6.5.1:

Conversions of semantically identical anonymous functions with the same (possibly empty) set of captured outer variable instances to the same delegate types are permitted (but not required) to return the same delegate instance. The term semantically identical is used here to mean that execution of the anonymous functions will, in all cases, produce the same effects given the same arguments.

like image 111
Jon Skeet Avatar answered Oct 07 '22 21:10

Jon Skeet