Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, are Lambda Expressions objects?

Tags:

c#

lambda

In C#, are lambda expressions objects? If so, what sort of object are they?

like image 332
Brian Warshaw Avatar asked Jun 25 '09 14:06

Brian Warshaw


1 Answers

Lambda expressions themselves only exist in source code. They don't have a type themselves, which is why the compiler always insists they're convert to a specific type.

That's why this code doesn't compile:

// No idea what type to convert to!
object x = y => y.Length;

But this does:

Func<string, int> x = y => y.Length;

Lambda expressions are always converted to either a delegate type or an expression tree type. Similarly, anonymous methods are always converted to a delegate type.

like image 106
Jon Skeet Avatar answered Oct 10 '22 15:10

Jon Skeet