Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert lambda expression to object directly?

Tags:

c#

object

lambda

I have to do through Action like this:

Action action = () => { ..// };
object o = action;

any way to do this:

object o = () =>{};  //this doesn't compile
like image 392
Benny Avatar asked Mar 11 '10 03:03

Benny


People also ask

Can we replace lambda expression with method reference?

out::println); The method references can only be used to replace a single method of the lambda expression. A code is more clear and short if one uses a lambda expression rather than using an anonymous class and one can use method reference rather than using a single function lambda expression to achieve the same.

What is the use of => in C#?

The => token is supported in two forms: as the lambda operator and as a separator of a member name and the member implementation in an expression body definition.

How do you convert lambda expression?

To convert a lambda expression to a named methodMove to the labda expression you want to convert. From the Refactor menu of the VisualAid choose To Named Method. Telerik® JustCode™ will replace the lambda expression with a method group and will add a new method.

Can a lambda expression be assigned to an object variable?

Yes, any lambda expression is an object in Java. It is an instance of a functional interface. We have assigned a lambda expression to any variable and pass it like any other object.


1 Answers

Weeeell, delegates are objects, but lambdas aren't.

This object o = (Action)(() => {}); will compile, but I don't know if it looks any better.

like image 122
Spike Avatar answered Sep 24 '22 04:09

Spike