7

For example, you can use lambda expressions in Visual Studio 2010, but still target .NET 2.0.

How does the compiler resolve lambda expressions to work with an older framework that does not include this feature?

Joseph Nields
  • 5,527
  • 2
  • 32
  • 48
  • They're anonymous delegates. Delegates have been there since 1.0, and "anonymous" simply means "the name is assigned by the compiler" – dmeglio May 06 '15 at 15:00
  • 1
    @dman2306 No, lambdas are not compiled into anonymous methods. Both features are compiled into traditional code. – Servy May 06 '15 at 15:01
  • @dman2306 VB.NET does not include anonymous delegates. – Joseph Nields May 06 '15 at 15:02
  • The important part is that you have the C# 3.0 compiler which allows you to use the lambda expressions, and compiles the code down to the specified targeted framework. – austin wernli May 06 '15 at 15:04
  • @JosephNields But the CLR/MSIL does. – dmeglio May 06 '15 at 15:06
  • 5
    Simply put, the lambda expression is a language feature, not a runtime feature. The generated MSIL generated by the compiler doesn't require any "new" (i.e. later than .NET 2.0) features of the runtime. – Jim Mischel May 06 '15 at 15:06
  • @Servy define "traditional code" – dmeglio May 06 '15 at 15:07
  • @dman2306 In this particular context, valid C# 2.0 code. – Servy May 06 '15 at 15:09
  • @Servy http://stackoverflow.com/questions/299703/delegate-keyword-vs-lambda-notation seems to suggest it will indeed create a delegate as I said. – dmeglio May 06 '15 at 15:09
  • 1
    @dman2306 Technically the phrase "anonymous delegate" isn't really sensible. Presumably you're referring to the construct: `delegate { someMethod(); }`. That is an anonymous method, which constructs a delegate. Sadly, the use of the `delegate` keyword here causes some confusion. Both anonymous methods, and lambdas, along with method groups, and some other possibilities, can be used to create delegates, which are just another type of object that happens to represent the ability to invoke a method on an instance. – Servy May 06 '15 at 15:15
  • @dman2306 Lambdas will be compiled into code that, at some point, creates a delegate, which is of course a necessity, as you're using it in a context where a delegate is needed; it couldn't compile if a delegate weren't created at some point. It doesn't rely on the anonymous method feature (aka the use of the `delegate` keyword) to accomplish that goal. – Servy May 06 '15 at 15:15

2 Answers2

9

Lambdas have no reliance on any of the newer framework features. A lambda, at the end of the day, only needs to be able to create a new class with fields, methods, and constructors, all of which is available in the 1.0 runtime/framework.

The following code:

int value = 42;
MyDelegate f = () => value;

Will be transformed into a new named type:

public class SomeRandomCompilerGeneratedNameGoesHere
{
    public int value;
    public int SomeGeneratedMethodName()
    {
        //the content of the anonymous method goes here
        return value;
    }
}

And will be used like so:

var closureClass = new SomeRandomCompilerGeneratedNameGoesHere();
closureClass.value = 42;
MyDelegate f = closureClass.SomeGeneratedMethodName;

Now, there are a few situations that don't require all of this; if there are no closed over values some of these steps can be elated, and optimizations added (i.e. the method can be made static, to avoid the creation of an object instance), but the transformation shown here is capable of mapping any valid C# lambda, and as you can see, the code it's transformed into would be valid even in C# 1.0.

Servy
  • 202,030
  • 26
  • 332
  • 449
5

Lambda expressions are compiler features. You don't need a framework or CLR support for it.

Compiler will create a method for you and perform the implicit delegate conversion and all those stuff for you. All you need is the new compiler in which the feature was implemented.

Most of the language features aren't tied with any version of .Net framework. Some of them just work; some of them can be tweaked using some tricks.

For example: Implicit delegate conversion, Collection Initializer, Object Initializer will work as it is. Extension methods can be used with some tricks.

Refer Jon's article, "Using C# 3 in .NET 2.0 and 3.0" section for more information.

FWIW, Using the same concept only BCL.Async library enables async-await feature in .Net 4.0 which was released along with .net 4.5.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189