views:

93

answers:

3

I have to do through Action like this:

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

any way to do this:

object o = () =>{};  //this doesn't compile
+3  A: 

What about:

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

Though I don't really know why you'd want to store it as an object in the first place...

Dean Harding
I am implement a message queue for a thread, command can be put in the queue for execution.
Benny
Hi, I'm kind of new here. What's the etiquette when someone posts an identical answer while I'm writing one. Should I just delete it? It seems kinds of superfluous now.
Spike
@Benny - If you can, consider using a generic structure like `Queue<Action>` so that the lambda expressions don't have to be cast as objects.
Greg
@Greg, well, sometime, i maybe want to put other object in the queue. so not just action.
Benny
@Spike - I'd leave your answer in place. It probably got more upvotes because you explained that delegates are objects.
Greg
@Spike, you don't have to delete it. that's how you get upvote. :)
Benny
+4  A: 

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

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

Spike
+2  A: 

Another option, not all that different:

object o = new Action(() => { });
Dan Tao
Actually, I like this one better. I mentally imagine the other solution as a constructor anyway. Might as well make it explicit.
Spike