简体   繁体   中英

What's the actual type of lambda in C#?

I read that C# lambdas can be imlicitly converted to Action or Func , but lambda cannot be executed directly Define a lambda function and execute it immediately For example :

int n = (()=>5)(); //doesn't work
int n = ((Func<int>)(()=>5))(); //works

So what is the actual type of lambda and why it cannot be directly called? Is it because C# type system is "weaker" than Haskell or Scala one?

A lambda expression doesn't have a type. It cannot, because any type in the .NET world that it could have, would also hard-code the type of the lambda's parameters and result. Now consider:

x => x + 1

What type could x have? What type will the result be? There isn't any single answer, and the lambda expression can indeed be converted to Func<int, int> , Func<double, double> , and many other delegate types with different parameters. Giving a lambda expression a type would disallow such expressions. C# did want to allow such expressions, so was designed not to give such expressions any type.

This is because () => 5 can be compatible with various delegate types (eg you may have a custom delegate that takes nothing and returns int ). And in order to create a delegate , compiler has to know the exact type. It can't just pick a random type that suits your needs. So that's why unless you cast it to an actual delegate type, you can't Invoke it.

In cases like where a method expects a delegate , that conversion is implicitly done by the compiler:

void Foo(Func<int> func) {  }

Foo(() => 5); 

And it is also important to know that delegates are actually classes behind the scenes. And each time you create a delegate instance, compiler creates an instance of that class . So either way you have to specify a type so the compiler will know which type to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM